Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize images with PHP, support PNG, JPG

Tags:

I am using this class:

class ImgResizer {  function ImgResizer($originalFile = '$newName') {     $this -> originalFile = $originalFile; } function resize($newWidth, $targetFile) {     if (empty($newWidth) || empty($targetFile)) {         return false;     }     $src = imagecreatefromjpeg($this -> originalFile);     list($width, $height) = getimagesize($this -> originalFile);     $newHeight = ($height / $width) * $newWidth;     $tmp = imagecreatetruecolor($newWidth, $newHeight);     imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);      if (file_exists($targetFile)) {         unlink($targetFile);     }     imagejpeg($tmp, $targetFile, 95); }  } 

Which works excellently, but it fails with png's, it creates a resized black image.

Is there a way to tweak this class to support png images?

like image 632
Toni Michel Caubet Avatar asked Nov 28 '12 02:11

Toni Michel Caubet


People also ask

How do I scale an image in PHP?

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().

How do I resize a PNG in HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How do I make an image smaller in PHP?

Images can be resized using ImageMagick or GD functions. If GD's functions are used, the size of the image file is also reduced when raw digital camera images are sampled.


2 Answers

function resize($newWidth, $targetFile, $originalFile) {      $info = getimagesize($originalFile);     $mime = $info['mime'];      switch ($mime) {             case 'image/jpeg':                     $image_create_func = 'imagecreatefromjpeg';                     $image_save_func = 'imagejpeg';                     $new_image_ext = 'jpg';                     break;              case 'image/png':                     $image_create_func = 'imagecreatefrompng';                     $image_save_func = 'imagepng';                     $new_image_ext = 'png';                     break;              case 'image/gif':                     $image_create_func = 'imagecreatefromgif';                     $image_save_func = 'imagegif';                     $new_image_ext = 'gif';                     break;              default:                      throw new Exception('Unknown image type.');     }      $img = $image_create_func($originalFile);     list($width, $height) = getimagesize($originalFile);      $newHeight = ($height / $width) * $newWidth;     $tmp = imagecreatetruecolor($newWidth, $newHeight);     imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);      if (file_exists($targetFile)) {             unlink($targetFile);     }     $image_save_func($tmp, "$targetFile.$new_image_ext"); } 
like image 61
P. Galbraith Avatar answered Oct 05 '22 15:10

P. Galbraith


I've written a class that will do just that and is nice and easy to use. It's called
PHP Image Magician

$magicianObj = new imageLib('racecar.jpg'); $magicianObj -> resizeImage(100, 200); $magicianObj -> saveImage('racecar_convertd.png', 100); 

It supports Read and Write (including converting) the following formats

  • jpg
  • png
  • gif
  • bmp

And can read only

  • psd's

Example

// Include PHP Image Magician library require_once('php_image_magician.php');  // Open JPG image $magicianObj = new imageLib('racecar.jpg');  // Resize to best fit then crop $magicianObj -> resizeImage(100, 200, 'crop');  // Save resized image as a PNG $magicianObj -> saveImage('racecar_small.png'); 
like image 24
Jarrod Avatar answered Oct 05 '22 13:10

Jarrod