Skip to content

Instantly share code, notes, and snippets.

@devflow
Created May 17, 2016 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devflow/2cc0eced28336c9c79a93f9d5af53f17 to your computer and use it in GitHub Desktop.
Save devflow/2cc0eced28336c9c79a93f9d5af53f17 to your computer and use it in GitHub Desktop.
function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop')
{
// check params
if (($source_file = FileHandler::exists($source_file)) === FALSE)
{
return;
}
$target_file = FileHandler::getRealPath($target_file);
if(!$resize_width)
{
$resize_width = 100;
}
if(!$resize_height)
{
$resize_height = $resize_width;
}
// retrieve source image's information
$imageInfo = getimagesize($source_file);
if(!FileHandler::checkMemoryLoadImage($imageInfo))
{
return FALSE;
}
list($width, $height, $type, $attrs) = $imageInfo;
if($width < 1 || $height < 1)
{
return;
}
switch($type)
{
case '1' :
$type = 'gif';
break;
case '2' :
$type = 'jpg';
break;
case '3' :
$type = 'png';
break;
case '6' :
$type = 'bmp';
break;
default :
return;
}
if(!$target_type)
{
$target_type = $type;
}
$target_type = strtolower($target_type);
// if original image is larger than specified size to resize, calculate the ratio
$width_per = ($resize_width > 0 && $width >= $resize_width) ? $resize_width / $width : 1;
$height_per = ($resize_height > 0 && $height >= $resize_height) ? $resize_height / $height : 1;
$per = NULL;
if($thumbnail_type == 'ratio')
{
$per = ($width_per > $height_per) ? $height_per : $width_per;
$resize_width = $width * $per;
$resize_height = $height * $per;
}
else if($thumbnail_type == 'center-crop')
{
$per = 1;
}
else
{
$per = ($width_per < $height_per) ? $height_per : $width_per;
}
// create temporary image with target size
$thumb = NULL;
if(function_exists('imagecreateTRUEcolor'))
{
$thumb = imagecreateTRUEcolor($resize_width, $resize_height);
}
else if(function_exists('imagecreate'))
{
$thumb = imagecreate($resize_width, $resize_height);
}
if(!$thumb)
{
return FALSE;
}
imagefilledrectangle($thumb, 0, 0, $resize_width - 1, $resize_height - 1, imagecolorallocate($thumb, 255, 255, 255));
// create temporary image having original type
$source = NULL;
switch($type)
{
case 'gif' :
if(function_exists('imagecreatefromgif'))
{
$source = @imagecreatefromgif($source_file);
}
break;
case 'jpeg' :
case 'jpg' :
if(function_exists('imagecreatefromjpeg'))
{
$source = @imagecreatefromjpeg($source_file);
}
break;
case 'png' :
if(function_exists('imagecreatefrompng'))
{
$source = @imagecreatefrompng($source_file);
}
break;
case 'wbmp' :
case 'bmp' :
if(function_exists('imagecreatefromwbmp'))
{
$source = @imagecreatefromwbmp($source_file);
}
break;
}
if(!$source)
{
imagedestroy($thumb);
return FALSE;
}
// resize original image and put it into temporary image
$new_width = (int) ($width * $per);
$new_height = (int) ($height * $per);
$x = 0;
$y = 0;
$s_x = 0;
$s_y = 0;
if($thumbnail_type == 'crop')
{
$x = (int) ($resize_width / 2 - $new_width / 2);
$y = (int) ($resize_height / 2 - $new_height / 2);
}else if($thumbnail_type == 'center-crop'){
$source_aspect_ratio = $width / $height;
$desired_aspect_ratio = $resize_width / $resize_height;
if ($source_aspect_ratio > $desired_aspect_ratio) {
$new_height = (int) ($resize_height);
$new_width = (int) ($resize_height * $source_aspect_ratio);
} else {
$new_width = (int) ($resize_width);
$new_height = (int) ($resize_width / $source_aspect_ratio);
}
$s_x = (int) (($new_width - $resize_width) / 2);
$s_y = (int) (($new_height - $resize_height) / 2);
}
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($thumb, $source, $x, $y, $s_x, $s_y, $new_width, $new_height, $width, $height);
}
else
{
imagecopyresized($thumb, $source, $x, $y, $s_x, $s_y, $new_width, $new_height, $width, $height);
}
// create directory
FileHandler::makeDir(dirname($target_file));
// write into the file
$output = NULL;
switch($target_type)
{
case 'gif' :
if(function_exists('imagegif'))
{
$output = imagegif($thumb, $target_file);
}
break;
case 'jpeg' :
case 'jpg' :
if(function_exists('imagejpeg'))
{
$output = imagejpeg($thumb, $target_file, 100);
}
break;
case 'png' :
if(function_exists('imagepng'))
{
$output = imagepng($thumb, $target_file, 9);
}
break;
case 'wbmp' :
case 'bmp' :
if(function_exists('imagewbmp'))
{
$output = imagewbmp($thumb, $target_file, 100);
}
break;
}
imagedestroy($thumb);
imagedestroy($source);
if(!$output)
{
return FALSE;
}
@chmod($target_file, 0644);
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment