Skip to content

Instantly share code, notes, and snippets.

@ijmorgado
Last active February 12, 2019 01:49
Show Gist options
  • Save ijmorgado/4662512 to your computer and use it in GitHub Desktop.
Save ijmorgado/4662512 to your computer and use it in GitHub Desktop.
This is a function to create thumbnail image(png, jpeg, jpg or gif) with specific height...is useful when we need to make tables with the same height for each row with image previews...if you really need the same width and whatever height, just change the order of params in the aspect ratio operations (lines 17 and 18)...
public function createThumbnail($source_folder, $thumbs_folder, $source_file, $extension, $thumbHeight){
if ($extension == 'gif') {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}else if($extension == 'jpg' || $extension == 'jpeg'){
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}else if ($extension == 'png') {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$img = $imgcreatefrom( $source_folder.$source_file.'.'.$extension );
$width = imagesx( $img );
$height = imagesy( $img );
// keep aspect ratio with these operations...
$new_width = floor( $width * ( $thumbHeight / $height ) );
$new_height = $thumbHeight;
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
if($extension == 'png'){
// Disable alpha mixing and set alpha flag if is a png file
imagealphablending($tmp_img, false);
imagesavealpha($tmp_img, true);
}
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$imgt( $tmp_img, $thumbs_folder.($source_file.'_'.$new_width.'x'.$new_height.'.'.$extension));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment