public function testImageResize() { $extension = 'png'; $sourceFile = __DIR__ . '/wiki_logo.' . $extension; $targetFile = __DIR__ . '/wiki_logo_new.' . $extension; $width = 50; $height = 50; ImageManager::resizeImage($sourceFile, $width, $height, $targetFile); $attributes = ImageManager::getImageAttributes($targetFile); $this->assertEquals($width, $attributes['width']); $this->assertEquals($height, $attributes['height']); $this->assertEquals($extension, $attributes['type']); unlink($targetFile); }
/** * Resizes an image to the given dimensions. If a target image is given, the file is saved to * the desired file. * * @param string $sourceImage full qualified path to the image file. * @param int $width width of the adjusted image. * @param int $height height of the adjusted image. * @param string $targetImage full qualified path to the target image. * @param int $jpgQuality The jpg quality (0-100). * @param int $pngCompression The png compression level (0-9). * * @throws InvalidArgumentException In case the applied image does not exist. * * @author Christian Achatz * @version * Version 0.1, 31.01.2009<br /> * Version 0.2, 01.05.2013 (Werner Liemberger: added pngCompression and bug-fix of the way the target image is saved. * Version 0.3, 11.10.2015 (ID#260: added safety check)<br /> */ public static function resizeImage($sourceImage, $width, $height, $targetImage = null, $jpgQuality = 80, $pngCompression = 0) { // check if the image is present on disk if (!file_exists($sourceImage)) { throw new InvalidArgumentException('[ImageManager::resizeImage()] The given image ("' . $sourceImage . '") does not exist! Hence, it cannot be adjusted.', E_USER_ERROR); } // ID#260: check whether target image is really null and NOT empty to avoid issues if ($targetImage !== null && empty($targetImage)) { throw new InvalidArgumentException('[ImageManager::resizeImage()] The target image name is empty! ' . 'Hence, it cannot be adjusted.', E_USER_ERROR); } // gather the current dimensions of the image $attributes = ImageManager::getImageAttributes($sourceImage); $sourceImageWidth = $attributes['width']; $sourceImageHeight = $attributes['height']; $sourceImageType = $attributes['type']; // create the current and the target image stream if ($sourceImageType == 'jpg') { $sourceImageStream = imagecreatefromjpeg($sourceImage); $targetImageStream = imagecreatetruecolor($width, $height); } elseif ($sourceImageType == 'gif') { $sourceImageStream = imagecreatefromgif($sourceImage); $targetImageStream = imagecreate($width, $height); } else { $sourceImageStream = imagecreatefrompng($sourceImage); $targetImageStream = imagecreate($width, $height); } // copy transparency if we resize a gif image if ($sourceImageType == 'gif') { // query the transparency color $transparentColor = imagecolortransparent($sourceImageStream); // copy palette imagepalettecopy($targetImageStream, $sourceImageStream); // fill with transparent color imagefill($targetImageStream, 0, 0, $transparentColor); // declare the transparent color as transparent :) imagecolortransparent($targetImageStream, $transparentColor); } // copy source image stream to target image stream and resize it imagecopyresized($targetImageStream, $sourceImageStream, 0, 0, 0, 0, $width, $height, $sourceImageWidth, $sourceImageHeight); if ($targetImage !== null) { // Get image type. The target image won't exist before the operation, // therefore the getimagesize() function from getImageAttributes // produces a read error. That's the reason why the image type is... $fileNamePartsArray = explode('.', $targetImage); $targetImageType = end($fileNamePartsArray); $targetImageType = strtolower($targetImageType); // sometimes the file extension name is written with an "e", so correct that $targetImageType = str_replace('jpeg', 'jpg', $targetImageType); switch ($targetImageType) { case 'jpg': imagejpeg($targetImageStream, $targetImage, $jpgQuality); break; case 'gif': imagegif($targetImageStream, $targetImage); break; case 'png': imagepng($targetImageStream, $targetImage, $pngCompression); break; default: throw new InvalidArgumentException('[ImageManager::resizeImage()] The targetImage extension ("' . $targetImageType . '") does not match "jpg", "png", or "gif"!', E_USER_ERROR); } } else { switch ($sourceImageType) { case 'jpg': imagejpeg($targetImageStream, '', $jpgQuality); break; case 'gif': imagegif($targetImageStream, ''); break; default: imagepng($targetImageStream, '', $pngCompression); } } // clean memory imagedestroy($targetImageStream); imagedestroy($sourceImageStream); }