Created
May 5, 2012 18:04
-
-
Save thefotolander/2604408 to your computer and use it in GitHub Desktop.
Poifox Quick Thumbnail Generator
This file contains hidden or 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 | |
/** | |
* Thumbnail generator function, | |
* used to generate jpeg thumbnails from gif, png and other jpegs; | |
* quite basic for now as it only crops square thumbnails but it will be | |
* improved in the future to support other aspect ratios. | |
* | |
* It does ugly upscaling. | |
* | |
* @param String $source_path Absolute path of the source image. | |
* @param String $dest_path Absolute path of the destination file. | |
* @param Int $size Optional thumbnail size in pixels. Default is 50px | |
* @param Int $q Optional quality parameter for jpeg compression. Default is 75% | |
*/ | |
function poifox_quick_thumbnail($source_path, $dest_path, $size = 50, $q = 75) { | |
// get image size | |
list($width, $height) = getimagesize($source_path); | |
if( $width == $height ) { | |
// Image is square | |
$x = $y = 0; | |
$newdim = $width; | |
} elseif ($width > $height) { | |
// Image is landscape | |
$x = ( $width / 2 ) - ( $height / 2 ); | |
$y = 0; | |
$newdim = $height; | |
} else { | |
// Image is portrait | |
$x = 0; | |
$y = ( $height / 2 ) - ( $width / 2 ); | |
$newdim = $width; | |
} | |
// Get the bytes from the original image and create a GD image with them | |
$original_bytes = file_get_contents($source_path); | |
$original = imagecreatefromstring($original_bytes); | |
// Create a placeholder for the cropped version | |
$copy = imagecreatetruecolor($size, $size); | |
// Copy pixels in the central area into $copy | |
imagecopyresampled($copy, $original, 0,0, $x, $y, $size, $size, $newdim, $newdim); | |
// Save the destination file to the $dest_path specified | |
// Saves as jpg with quality 75 | |
// Returns true on success, false on failure | |
return imagejpeg($copy, $dest_path, $q); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment