Last active
February 12, 2019 01:49
-
-
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)...
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
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