Skip to content

Instantly share code, notes, and snippets.

@devpilot
Created January 17, 2012 23:46
Show Gist options
  • Save devpilot/1629827 to your computer and use it in GitHub Desktop.
Save devpilot/1629827 to your computer and use it in GitHub Desktop.
Create image thumb
<?php
/**
* Create thumbnail from image file.
* <p>Supported format ".jpg", ".jpeg", ".png", ".gif"</p>
* @param string $directory
* Path to source directory
* @param string $image
* name of image file including extention
* @param string $destination
* path to where thumb image will be saved
* @author Pilot
*/
function createThumbnail($directory, $image, $destination) {
if (file_exists($directory.$image)) {
if (preg_match('/[.]jpg$/', $image) || preg_match('/[.]jpeg$/', $image)) {
$im = imagecreatefromjpeg($directory . $image);
} else if (preg_match('/[.]gif$/', $image)) {
$im = imagecreatefromgif($directory . $image);
} else if (preg_match('/[.]png$/', $image)) {
$im = imagecreatefrompng($directory . $image);
}
$ox = imagesx($im);
$oy = imagesy($im);
$thumb_width = 161;
$thumb_height=120;
$ny = floor($oy * ($thumb_width / $ox));
if ($ny<121) {
$nx = floor($ox*($thumb_height/$oy));
$nm = imagecreatetruecolor($nx, $thumb_height);
imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $thumb_height, $ox, $oy);
} else {
$nm = imagecreatetruecolor($thumb_width, $ny);
imagecopyresized($nm, $im, 0, 0, 0, 0, $thumb_width, $ny, $ox, $oy);
}
$image = explode('.', $image);
imagejpeg($nm, $destination . $image[0].'.'.'jpg', 30);
imagedestroy($nm);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment