Last active
August 25, 2023 07:13
-
-
Save georgymh/71669b0b16997049e0ee to your computer and use it in GitHub Desktop.
PHP Image resizing and cropping methods.
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 | |
/* | |
Creates a thumbnail keeping the aspect ratio of the source image, and | |
saves it to a destination $dest. | |
Credits: http://davidwalsh.name/create-image-thumbnail-php | |
*/ | |
function make_thumb($src, $dest, $desired_width) { | |
$info = getimagesize($src); | |
$imgtype = image_type_to_mime_type($info[2]); | |
/* read the source image */ | |
switch ($imgtype) { | |
case 'image/jpeg': | |
$source_image = imagecreatefromjpeg($src); | |
break; | |
case 'image/gif': | |
$source_image = imagecreatefromgif($src); | |
break; | |
case 'image/png': | |
$source_image = imagecreatefrompng($src); | |
break; | |
default: | |
die('Invalid image type.'); | |
} | |
$width = imagesx($source_image); | |
$height = imagesy($source_image); | |
/* find the "desired height" of this thumbnail, relative to the desired width */ | |
$desired_height = floor($height * ($desired_width / $width)); | |
/* create a new, "virtual" image */ | |
$virtual_image = imagecreatetruecolor($desired_width, $desired_height); | |
/* copy source image at a resized size */ | |
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); | |
/* create the physical thumbnail image to its destination */ | |
imagejpeg($virtual_image, $dest); | |
} | |
/* | |
Creates and returns a thumbnail, keeping the aspect ratio of the source image. | |
Credits: http://php.net/manual/en/function.imagejpeg.php | |
*/ | |
function create_thumb($src, $desired_width) { | |
$info = getimagesize($src); | |
$imgtype = image_type_to_mime_type($info[2]); | |
/* read the source image */ | |
switch ($imgtype) { | |
case 'image/jpeg': | |
$source_image = imagecreatefromjpeg($src); | |
break; | |
case 'image/gif': | |
$source_image = imagecreatefromgif($src); | |
break; | |
case 'image/png': | |
$source_image = imagecreatefrompng($src); | |
break; | |
default: | |
die('Invalid image type.'); | |
} | |
$width = imagesx($source_image); | |
$height = imagesy($source_image); | |
/* find the "desired height" of this thumbnail, relative to the desired width */ | |
$desired_height = floor($height * ($desired_width / $width)); | |
/* create a new, "virtual" image */ | |
$virtual_image = imagecreatetruecolor($desired_width, $desired_height); | |
/* copy source image at a resized size */ | |
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); | |
/* | |
* imageXXX() only has two options, save as a file, or send to the browser. | |
* It does not provide you the oppurtunity to manipulate the final GIF/JPG/PNG file stream | |
* So I start the output buffering, use imageXXX() to output the data stream to the browser, | |
* get the contents of the stream, and use clean to silently discard the buffered contents. | |
*/ | |
ob_start(); | |
/* create the thumbnail image */ | |
imagejpeg($virtual_image, null); | |
$final_image = ob_get_contents(); | |
ob_end_clean(); | |
return $final_image; | |
} | |
/* | |
Crops an image to the specified thumbnail size, starting the cropping from $x and $y. | |
Credits: http://stackoverflow.com/questions/3255773/php-crop-image-to-fix-width-and-height-without-losing-dimension-ratio | |
*/ | |
function cropImageToSquare($imgSrc, $imgDest, $thumbSize, $x = 0, $y = 0) { | |
//getting the image dimensions | |
list($width, $height) = getimagesize($imgSrc); | |
//saving the image into memory (for manipulation with GD Library) | |
$myImage = imagecreatefromjpeg($imgSrc); | |
// calculating the part of the image to use for thumbnail | |
$smallestSide = min($height, $width); | |
// copying the part into thumbnail | |
$thumb = imagecreatetruecolor($thumbSize, $thumbSize); | |
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); | |
//final output | |
header('Content-type: image/jpeg'); | |
imagejpeg($thumb, $imgDest); | |
} | |
/* | |
Resizes an image and puts it on top of a white, square background. | |
Returns true if successful, false otherwise. | |
Credits: http://stackoverflow.com/questions/4478223/how-do-i-fill-white-background-while-resize-image | |
*/ | |
function resizeImage($source_image, $destination, $tn_w, $tn_h, $quality = 100) { | |
$info = getimagesize($source_image); | |
$imgtype = image_type_to_mime_type($info[2]); | |
#assuming the mime type is correct | |
switch ($imgtype) { | |
case 'image/jpeg': | |
$source = imagecreatefromjpeg($source_image); | |
break; | |
case 'image/gif': | |
$source = imagecreatefromgif($source_image); | |
break; | |
case 'image/png': | |
$source = imagecreatefrompng($source_image); | |
break; | |
default: | |
die('Invalid image type.'); | |
} | |
#Figure out the dimensions of the image and the dimensions of the desired thumbnail | |
$src_w = imagesx($source); | |
$src_h = imagesy($source); | |
#Do some math to figure out which way we'll need to crop the image | |
#to get it proportional to the new size, then crop or adjust as needed | |
$x_ratio = $tn_w / $src_w; | |
$y_ratio = $tn_h / $src_h; | |
if (($src_w <= $tn_w) && ($src_h <= $tn_h)) { | |
$new_w = $src_w; | |
$new_h = $src_h; | |
} elseif (($x_ratio * $src_h) < $tn_h) { | |
$new_h = ceil($x_ratio * $src_h); | |
$new_w = $tn_w; | |
} else { | |
$new_w = ceil($y_ratio * $src_w); | |
$new_h = $tn_h; | |
} | |
$newpic = imagecreatetruecolor(round($new_w), round($new_h)); | |
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h); | |
$final = imagecreatetruecolor($tn_w, $tn_h); | |
$backgroundColor = imagecolorallocate($final, 255, 255, 255); | |
imagefill($final, 0, 0, $backgroundColor); | |
//imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h); | |
imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h); | |
if (imagejpeg($final, $destination, $quality)) { | |
return true; | |
} | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment