Created
August 17, 2020 17:37
-
-
Save lazyjerry/44af6edd8e803aabc93aada2c49cfb07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
private 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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment