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?
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().
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.
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.
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"); }
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
And can read only
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With