Skip to content

Instantly share code, notes, and snippets.

@ZachMoreno
Created February 21, 2012 07:34
Show Gist options
  • Save ZachMoreno/1874854 to your computer and use it in GitHub Desktop.
Save ZachMoreno/1874854 to your computer and use it in GitHub Desktop.
Create Square Thumbnails
<?php
function create_square_thumbnails($userfile_name, $userfile_type)
{
//get the file and figure out what type of file it is and some of its attributes
switch ($userfile_type)
{
case "image/pjpeg": $tempbig = imagecreatefromjpeg(IMG_PATH.$userfile_name); break;
case "image/jpeg": $tempbig = imagecreatefromjpeg(IMG_PATH.$userfile_name); break;
case "image/jpg": $tempbig = imagecreatefromjpeg(IMG_PATH.$userfile_name); break;
case "image/gif": $tempbig = imagecreatefromgif(IMG_PATH.$userfile_name); break;
case "image/x-png": $tempbig = imagecreatefrompng(IMG_PATH.$userfile_name); break;
case "image/png": $tempbig = imagecreatefrompng(IMG_PATH.$userfile_name); break;
}
$bigsize = getimagesize(IMG_PATH.$userfile_name);
$bigwidth = $bigsize[0];
$bigheight = $bigsize[1];
$tempsmall = imagecreatetruecolor(100, 100);
if($bigwidth > $bigheight)
{
$xstart = ($bigwidth/2)-($bigheight/2);
imagecopyresampled($tempsmall, $tempbig, 0, 0, $xstart, 0, 100, 100, $bigheight, $bigheight); //create temp image to be moved
switch ($userfile_type)
{
case "image/pjpeg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/jpeg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/jpg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/gif": imagegif($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/x-png": imagepng($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/png": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
}
Imagedestroy($tempbig);
Imagedestroy($tempsmall); //destory temp image
}
elseif($bigheight > $bigwidth)
{
$ystart = ($bigheight/2)-($bigwidth/2);
imagecopyresampled($tempsmall, $tempbig, 0, 0, 0, $ystart, 100, 100, $bigwidth, $bigwidth);
//get the file and figure out what type of file it is and some of its attributes
switch ($userfile_type)
{
case "image/pjpeg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/jpeg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/jpg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/gif": imagegif($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/x-png": imagepng($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/png": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
}
Imagedestroy($tempbig);
Imagedestroy($tempsmall); //destory temp images
}
else
{
imagecopyresampled($tempsmall, $tempbig, 0, 0, 0, 0, 100, 100, $bigwidth, $bigheight);
switch ($userfile_type)
{
case "image/pjpeg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/jpeg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/jpg": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/gif": imagegif($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/x-png": imagepng($tempsmall, THUMB_PATH.$userfile_name, 95); break;
case "image/png": imagejpeg($tempsmall, THUMB_PATH.$userfile_name, 95); break;
}
Imagedestroy($tempbig);
Imagedestroy($tempsmall); //destory temp images
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment