Skip to content

Instantly share code, notes, and snippets.

@pedroppinheiro
Last active June 21, 2019 17:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pedroppinheiro/7a039da05fd9a1bc4182 to your computer and use it in GitHub Desktop.
Save pedroppinheiro/7a039da05fd9a1bc4182 to your computer and use it in GitHub Desktop.
This php funtion creates a thumbnail of an image. It supports JPEG, JPG, PNG and GIF formats and can be used to define a specific background color, including transparent when dealing with PNG.
<?php
/**
* This code is an improvement over Alex's code that can be found here -> http://stackoverflow.com/a/11376379
*
* This funtion creates a thumbnail with size $thumbnail_width x $thumbnail_height.
* It supports JPG, PNG and GIF formats. The final thumbnail tries to keep the image proportion.
*
* It has been pointed out that if the thumbnail already exists it doesn't get overwritten, but the method still returns true.
* so keep an eye on that.
*
* Example of usage:
*
* <code>
* require_once 'create_thumbnail.php';
*
* $success = createThumbnail(__DIR__.DIRECTORY_SEPARATOR.'image.jpg', __DIR__.DIRECTORY_SEPARATOR.'image_thumb.jpg', 60, 60, array(255,255,255)); // creates a thumbnail called image_thumb.jpg with 60x60 in size and with a white background
*
* echo $success ? 'thumbnail was created' : 'something went wrong';
* </code>
*
* @author Pedro Pinheiro (https://github.com/pedroppinheiro).
* @param string $filepath The image's complete path. Example: C:\xampp\htdocs\project\image.jpg
* @param string $thumbpath The path to create the thumbnail + name of the thumbnail. Example: C:\xampp\htdocs\project\image_thumbnail.jpg
* @param int $thumbnail_width Width of the thumbnail. Only integers allowed.
* @param int $thumbnail_height Height of the thumbnail. Only integers allowed.
* @param int[int] | 'transparent' An array containing the values of red, green, and blue to be used as the image's background color, or use the string 'transparent' to define the background as transparent (only applicable to png images). This parameter is optional, so if no value is provided, then the default background will be black.
* @return boolean Returns true if the thumbnail was created successfully, false otherwise.
*/
function createThumbnail($filepath, $thumbpath, $thumbnail_width, $thumbnail_height, $background=false) {
list($original_width, $original_height, $original_type) = getimagesize($filepath);
if ($original_width > $original_height) {
$new_width = $thumbnail_width;
$new_height = intval($original_height * $new_width / $original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width * $new_height / $original_height);
}
$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);
if ($original_type === 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
} else if ($original_type === 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
} else if ($original_type === 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
} else {
return false;
}
$old_image = $imgcreatefrom($filepath);
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); // creates new image, but with a black background
// figuring out the color for the background
if(is_array($background) && count($background) === 3) {
list($red, $green, $blue) = $background;
$color = imagecolorallocate($new_image, $red, $green, $blue);
imagefill($new_image, 0, 0, $color);
// apply transparent background only if is a png image
} else if($background === 'transparent' && $original_type === 3) {
imagesavealpha($new_image, TRUE);
$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
imagefill($new_image, 0, 0, $color);
}
imagecopyresampled($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
$imgt($new_image, $thumbpath);
return file_exists($thumbpath);
}
Copy link

ghost commented Apr 25, 2016

Hi, i wanted to thank you for providing this, i am using it on one of my plugins and it works great. Just a little nip and tuck here and there to fit my usage is all that was needed as we dont process gif images. but very easy to get working they way we needed it to. I did leave the attribution part on the top of the function even though that github site is no longer valid. @author Pedro Pinheiro (https://github.com/PedroVPP). But some of the instructions and other stuff i removed, no need for it.

Thank you again and really appreciate it :) nice work all around!

dave :)

@pedroppinheiro
Copy link
Author

@durangod

You're welcome mate. Glad I could help! :)
Also, thanks for letting me know about the broken link.

@BrockleyJohn
Copy link

Thanks Pedro. A note of caution to other users - if the thumbnail already exists it doesn't get overwritten but the function appears to succeed (returns true).

@zaw-hlaing-bwar
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment