Created
December 17, 2012 13:39
-
-
Save arisetyo/4318339 to your computer and use it in GitHub Desktop.
Thumbnail generator using PHP
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 | |
function CreateThumb($file, $photo_url) { | |
$square_size = 160; | |
$quality = 100; | |
//GET THE SOURCE IMAGE'S METADATA | |
if($photo_url!="") list($width, $height, $type, $attr) = getimagesize($photo_url); | |
//CHECK SOURCE IMAGE'S DIMENSION | |
if($width>$height) { | |
$width_t = $square_size; | |
//ADJUST THE RATIO OF THE THUMBNAIL | |
$height_t = round($height/$width*$square_size); | |
//SET THE OFFSET SO THE IMAGE IS PLACED IN THE CENTER OF THE THUMBNAIL | |
$off_y = ceil(($width_t-$height_t)/2); | |
$off_x = 0; | |
} elseif($height>$width) { | |
$height_t = $square_size; | |
$width_t = round($width/$height*$square_size); | |
$off_x = ceil(($height_t-$width_t)/2); | |
$off_y = 0; | |
} else { | |
$width_t = $height_t = $square_size; | |
$off_x = $off_y = 0; | |
} | |
//GET THE PIXELS OF THE ORIGINAL IMAGE | |
$thumb = imagecreatefromjpeg($photo_url); | |
$thumb_p = imagecreatetruecolor($square_size, $square_size); | |
//SET THE BACKGROUND TO BLACK | |
$bg = imagecolorallocate($thumb_p, 0, 0, 0); | |
//REDRAW THE IMAGE | |
imagefill($thumb_p, 0, 0, $bg); | |
imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height); | |
//CREATE THE THUMBNAIL FILE | |
imagejpeg($thumb_p, "thumb_".$file.".jpg", $quality); | |
//CLEAN UP | |
imagedestroy($thumb_p); | |
imagedestroy($thumb); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment