Current background is black. How to change the color to be white?
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
$width = $info[0];
$height = $info[1];
$x_ratio = $tn_w / $src_w;
$y_ratio = $tn_h / $src_h;
if (($x_ratio * $height) < $tn_w) {
$new_h = ceil($x_ratio * $height);
$new_w = $tn_w;
} else {
$new_w = ceil($y_ratio * $width);
$new_h = $tn_h;
}
$x_mid = $new_w / 2;
$y_mid = $new_h / 2;
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);
#if we need to add a watermark
if ($wmsource) {
#find out what type of image the watermark is
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
die('Invalid watermark type.');
}
#if we're adding a watermark, figure out the size of the watermark
#and then place the watermark image on the bottom right of the image
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
}
if (imagejpeg($final, $destination, $quality)) {
return true;
}
return false;
}
Black & White
$final = imagecreatetruecolor($tn_w, $tn_h);
$backgroundColor = imagecolorallocate($final, 255, 255, 255);
imagefill($final, 0, 0, $backgroundColor);
//imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);
imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
Here is your whole script (tested with portrait, landscape and square jpg):
<?php
function resize($source_image, $destination, $tn_w, $tn_h, $quality = 100, $wmsource = false)
{
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
$x_ratio = $tn_w / $src_w;
$y_ratio = $tn_h / $src_h;
if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {
$new_w = $src_w;
$new_h = $src_h;
} elseif (($x_ratio * $src_h) < $tn_h) {
$new_h = ceil($x_ratio * $src_h);
$new_w = $tn_w;
} else {
$new_w = ceil($y_ratio * $src_w);
$new_h = $tn_h;
}
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
$backgroundColor = imagecolorallocate($final, 255, 255, 255);
imagefill($final, 0, 0, $backgroundColor);
//imagecopyresampled($final, $newpic, 0, 0, ($x_mid - ($tn_w / 2)), ($y_mid - ($tn_h / 2)), $tn_w, $tn_h, $tn_w, $tn_h);
imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
#if we need to add a watermark
if ($wmsource) {
#find out what type of image the watermark is
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
die('Invalid watermark type.');
}
#if we're adding a watermark, figure out the size of the watermark
#and then place the watermark image on the bottom right of the image
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
imagecopy($final, $watermark, $tn_w - $wm_w, $tn_h - $wm_h, 0, 0, $tn_w, $tn_h);
}
if (imagejpeg($final, $destination, $quality)) {
return true;
}
return false;
}
resize('teszt2.jpg', 'out.jpg', 100, 100);
?>
<img src="out.jpg">
This is working for me. Although it logically seems like it should fill the whole image with the $bgcolor, it only fills the parts that are "behind" the resampled image.
imagecopyresampled($resized_image, $original_image, $xoffset, $yoffset, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
$bgcolor = imagecolorallocate($resized_image, $red, $green, $blue);
imagefill($resized_image, 0, 0, $bgcolor);
Related
Currently, I am resizing avatars on my server using PHP's resize_crop_image. The code below takes an image, crops and resizes it so that it's exactly 500px X 500px square.
resize_crop_image(500, 500, $filename, $filename);
But I'd like to move this process from the server to the flutter app. Most of the plugins I'm seeing on pub.dev are overkill with the user cropping the image using a crop tool. But I'd like this function to happen automatically the way I currently do it in PHP.
Here's the resize_crop_image code;
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 100){
$imgsize = getimagesize($source_file);
$width = $imgsize[0];
$height = $imgsize[1];
$mime = $imgsize['mime'];
switch($mime){
case 'image/gif':
$image_create = "imagecreatefromgif";
$image = "imagegif";
break;
case 'image/png':
$image_create = "imagecreatefrompng";
$image = "imagepng";
$quality = 10;
break;
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$quality = 100;
break;
default:
return false;
break;
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
$src_img = $image_create($source_file);
$width_new = $height * $max_width / $max_height;
$height_new = $width * $max_height / $max_width;
//if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
if($width_new > $width){
//cut point by height
$h_point = (($height - $height_new) / 2);
//copy image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
}else{
//cut point by width
$w_point = (($width - $width_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
?>
You could use this library: flutter_native_image for resizing/croping your images.
Important to take a look at the other parameters of this function, you can probably customize the way the image will be cropped.
Something like that:
ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
File croppedFile = await FlutterNativeImage.cropImage(file.path, originX, originY, 500, 500);
originX/originY: x/y position for the cut.
Im trying to create with php a 4:3 image with any image uploaded by user. No matter the image original size, i want to fill the background with a blurred copy of the same image.
This is the code i use (from István Ujj-Mészáros):
function resize($source_image, $destination, $tn_w, $tn_h, $quality = 90) {
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);
#assuming the mime type is correct
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
#Figure out the dimensions of the image and the dimensions of the desired thumbnail
$src_w = imagesx($source);
$src_h = imagesy($source);
#Do some math to figure out which way we'll need to crop the image
#to get it proportional to the new size, then crop or adjust as needed
$x_ratio = $tn_w / $src_w;
$y_ratio = $tn_h / $src_h;
if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {
$new_w = $src_w;
$new_h = $src_h;
} elseif (($x_ratio * $src_h) < $tn_h) {
$new_h = ceil($x_ratio * $src_h);
$new_w = $tn_w;
} else {
$new_w = ceil($y_ratio * $src_w);
$new_h = $tn_h;
}
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
// This code fill with green color
//$backgroundColor = imagecolorallocate($final, 0, 255, 0);
//imagefill($final, 0, 0, $backgroundColor);
imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);
// This code generates a blurred image
# ****************************************************
//for ($x=1; $x <=2; $x++){
// imagefilter($final, IMG_FILTER_GAUSSIAN_BLUR, 999);
//}
//imagefilter($final, IMG_FILTER_SMOOTH,99);
//imagefilter($final, IMG_FILTER_BRIGHTNESS, 10);
# ****************************************************
if (imagejpeg($final, $destination, $quality)) {
return true;
}
return false;
}
// targetFilePath contains the folder an filename
resize($targetFilePath,$targetFilePath,640,480,90);
The result is like this image:
my result until now
What do i need?
The result that i hope
Please any idea will be welcome.
Thank you in advance!!!
I enhanced #mhuenchul code and it works now whatever the image size is.
function image_blurred_bg($image, $dest, $width, $height){
try{
$info = getimagesize($image);
} catch (Exception $e){
return false;
}
$mimetype = image_type_to_mime_type($info[2]);
switch ($mimetype) {
case 'image/jpeg':
$image = imagecreatefromjpeg($image);
break;
case 'image/gif':
$image = imagecreatefromgif($image);
break;
case 'image/png':
$image = imagecreatefrompng($image);
break;
default:
return false;
}
$wor = imagesx($image);
$hor = imagesy($image);
$back = imagecreatetruecolor($width, $height);
$maxfact = max($width/$wor, $height/$hor);
$new_w = $wor*$maxfact;
$new_h = $hor*$maxfact;
imagecopyresampled($back, $image, -(($new_w-$width)/2), -(($new_h-$height)/2), 0, 0, $new_w, $new_h, $wor, $hor);
// Blur Image
for ($x=1; $x <=40; $x++){
imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($back, IMG_FILTER_SMOOTH,99);
imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
$minfact = min($width/$wor, $height/$hor);
$new_w = $wor*$minfact;
$new_h = $hor*$minfact;
$front = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($front, $image, 0, 0, 0, 0, $new_w, $new_h, $wor, $hor);
imagecopymerge($back, $front,-(($new_w-$width)/2), -(($new_h-$height)/2), 0, 0, $new_w, $new_h, 100);
// output new file
imagejpeg($back,$dest,90);
imagedestroy($back);
imagedestroy($front);
return true;
}
There are many approaches, but I would suggest you use imagecopymerge instead of imagecopy. You provide the destination and source coordinates as well as the source size and voila!
However note that this way you won't keep transparency (for GIF/PNG, if present). To do it, have a look at this comment of Sina Salek in the PHP documentation: PNG ALPHA CHANNEL SUPPORT for imagecopymerge().
But I would agree with #LeeKowalkowski - in the long run, you should consider migrating to ImageMagick for many reasons - image quality above all.
EDIT: I forgot to mention, before merging, set transparent colour (the extended canvas) with imagecolortransparent. Then, Sina Salek's comment in PHP documentation (the link above).
Ok, i did my work and studied more about php image commands. I wrote a solution that works very well.
<?php
$image = "01.jpg";
image_web($image, 700, 525); // I need final images 700x525 (4:3)
echo '<img src="00.jpg"/>';
function image_web($image, $width, $height){ // Get the image source and the final desire dimensions
$info = getimagesize($image);
//$mimetype = $info['mime']; // other way to get mimetype
$mimetype = image_type_to_mime_type($info[2]);
$allowTypes = array('image/jpeg','image/png','image/gif');
if(in_array($mimetype, $allowTypes)){
switch ($mimetype) {
case 'image/jpeg':
$image = imagecreatefromjpeg($image);
break;
case 'image/gif':
$image = imagecreatefromgif($image);
break;
case 'image/png':
$image = imagecreatefrompng($image);
break;
default:
die('Invalid image type.');
}
} else {
echo 'Is not a image';
}
$image2 = $image; // Save a copy to be used for front image
$wor = imagesx($image); // Get original image width
$hor = imagesy($image); // Get original image height
if ($hor >= $wor){ // If is vertical*******************************************************************************************************
if ($wor >= $width){ // If image source is bigger than final desire dimensions
$hcenter = ($hor/2)-($height/2); // center image source in height
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $wor, $hor, $wor, $hor);
} else { // If image source is not bigger than final desire dimensions
$hcenter = ($hor/2)-($height/2); // center image source in height
$hnu = ($hor*$width)/$wor;
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
}
} else { // If is portrait rectangular****************************************************************************************************
$ratio = $wor/$hor;
if($ratio > 1.3333333){ // If is portrait larger than 4:3
$wnu = ($wor*$height)/$hor;
$wcenter = ($wnu/2)-($width/2); // center image in width
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, -$wcenter, 0, 0, 0, $wnu, $height, $wor, $hor);
} else { // If portrait is not larger than 4:3
$hnu = ($wor*$height)/$hor;
$hcenter = ($hnu/2)-($height/2); // center image source in height
$back = imagecreatetruecolor(round($width), round($height));
imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
}
}
// Blur Image
for ($x=1; $x <=40; $x++){
imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($back, IMG_FILTER_SMOOTH,99);
imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
// Getting the dimensions of the image
$src_w = imagesx($image2);
$src_h = imagesy($image2);
// Do some math to figure out which way we'll need to crop the image
// to get it proportional to the new size, then crop or adjust as needed
$x_ratio = $width / $src_w;
$y_ratio = $height / $src_h;
if (($src_w <= $width) && ($src_h <= $height)) {
$new_w = $src_w;
$new_h = $src_h;
} elseif (($x_ratio * $src_h) < $height) {
$new_h = ceil($x_ratio * $src_h);
$new_w = $width;
} else {
$new_w = ceil($y_ratio * $src_w);
$new_h = $height;
}
$front = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($front, $image2, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
if ($new_h >= $new_w){ // If is vertical image
$wctr = ($new_w/2)-($width/2);
imagecopymerge($back, $front,-$wctr, 0, 0, 0, $new_w, $new_h, 100);
} else { // if is portrait
$hctr = ($new_h/2)-($height/2);
imagecopymerge($back, $front,0, -$hctr, 0, 0, $new_w, $new_h, 100);
}
// output new file
imagejpeg($back,'00.jpg',90);
imagedestroy($back);
imagedestroy($front);
//*********************************************************************************
/**
Do other actions like send ajax responses, save in database, etc
**/
}
?>
First, I'm not master of PHP. I'm using a function for resize and crop images. It's working perfectly until I upload a transparent png. :)
It saves the png with black background. I found some answers on stackoverflow but I can't combine it with my codes.
Here is my function:
//resize and crop image
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 90){
$imgsize = getimagesize($source_file);
$width = $imgsize[0];
$height = $imgsize[1];
$mime = $imgsize['mime'];
switch($mime){
case 'image/gif':
$image_create = "imagecreatefromgif";
$image = "imagegif";
$format = "gif";
break;
case 'image/png':
$image_create = "imagecreatefrompng";
$image = "imagepng";
$quality = 7;
$format = "png";
break;
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$format = "jpg";
break;
default:
return false;
break;
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
$src_img = $image_create($source_file);
$width_new = $height * $max_width / $max_height;
$height_new = $width * $max_height / $max_width;
//if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
if($width_new > $width){
//cut point by height
$h_point = (($height - $height_new) / 2);
//copy image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
}else{
//cut point by width
$w_point = (($width - $width_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
}
// you can ignore these 4 lines. I'm using it for change the name.
$nameforimage = rand('11111111', '9999999999');
$nameforimage2 = rand('11111111', '9999999999');
$newname = $nameforimage."_".$nameforimage2;
$newdir = $dst_dir."".$newname.".".$format;
$image($dst_img, $newdir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
return $newname.".".$format;
}
EDIT:
Okay I've found a solution.
Just add these lines:
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $max_width, $max_height, $transparent);
After this line:
$dst_img = imagecreatetruecolor($max_width, $max_height);
You have to enable saving the alpha channel.
It can be done with imagesavealpha(), e.g.:
// As per the manual, alpha blending must be disabled
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
I am using php GD image for the image upload but I want to add watermark to resized image. I found some code to add a watermark but I couldn't make it work. I need some hints.
function resizeImage($CurWidth, $CurHeight, $MaxSize, $DestFolder, $SrcImage, $Quality, $ImageType)
{
//Check Image size is not 0
if ($CurWidth <= 0 || $CurHeight <= 0) {
return false;
}
//Construct a proportional size of new image
$ImageScale = min($MaxSize / $CurWidth, $MaxSize / $CurWidth);
$NewWidth = ceil($ImageScale * $CurWidth);
$NewHeight = ceil($ImageScale * $CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
$watermark_png_file = 'watermark.png';
//calculate center position of watermark image
$watermark_left = ($NewWidth / 2) - (300 / 2); //watermark left
$watermark_bottom = ($NewHeight / 2) - (100 / 2); //watermark bottom
$watermark = imagecreatefrompng($watermark_png_file); //watermark image
//use PHP imagecopy() to merge two images.
imagecopy($NewCanves, $watermark, $watermark_left, $watermark_bottom, 0, 0, 300, 100); //merge image
// Resize Image
if (imagecopyresampled($NewCanves, $SrcImage, 0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch (strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves, $DestFolder);
break;
case 'image/gif':
imagegif($NewCanves, $DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves, $DestFolder, $Quality);
break;
default:
return false;
}
//Destroy image, frees memory
if (is_resource($NewCanves)) imagedestroy($NewCanves);
return true;
}
}
I have tried this code but it doesn't work:
function generate_watermarked_image($ImageType, $CurWidth, $CurHeight, $paddingFromBottomRight = 0)
{
$watermarkFileLocation = 'logo.png';
$watermarkImage = imagecreatefrompng($watermarkFileLocation);
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);
$originalImage = imagecreatefromstring($ImageType);
$destX = $CurWidth - $watermarkWidth - $paddingFromBottomRight;
$destY = $CurHeight - $watermarkHeight - $paddingFromBottomRight;
// creating a cut resource
$cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight);
// copying that section of the background to the cut
imagecopy($cut, $originalImage, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight);
// placing the watermark now
imagecopy($cut, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);
// merging both of the images
imagecopymerge($originalImage, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100);
}
I have implemented a file upload for pictures on my page, and tried to somehow generate thumbnails with the intention of clicking them to view via fancybox. The upload works but my function to create a thumbnail doesn't.
(This is included in my upload.php, right after "move_uploaded_file":
<?php
$src = $subdir.$fileupload['name'];
function make_thumb($src)
{
$source_image = imagecreatefromjpeg($src); //For testing purposes only jpeg now
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_width = 220;
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
header("Content-type: image/jpeg");
imagejpeg($virtual_image, realpath('./Thumbnails/filename.jpg')); //Temporary filename, will be changed
}
?>
Just FYI, this is an assignment and since I am a php beginner, I did use google, but can't find the problem in my case. Maybe my understanding of php is lacking too much.
Use this img_resize function, it is good for the most popular image formats
function img_resize($src, $dest, $width, $height, $rgb = 0xFFFFFF, $quality = 100)
{
if (!file_exists($src))
return false;
$size = getimagesize($src);
if ($size === false)
return false;
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$icfunc = "imagecreatefrom" . $format;
if (!function_exists($icfunc))
return false;
$x_ratio = $width / $size[0];
$y_ratio = $height / $size[1];
$ratio = min($x_ratio, $y_ratio);
$use_x_ratio = ($x_ratio == $ratio);
$new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
$new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
$new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
$new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
$isrc = $icfunc($src);
$idest = imagecreatetruecolor($width, $height);
imagefill($idest, 0, 0, $rgb);
if (($format == 'gif') or ($format == 'png')) {
imagealphablending($idest, false);
imagesavealpha($idest, true);
}
if ($format == 'gif') {
$transparent = imagecolorallocatealpha($idest, 255, 255, 255, 127);
imagefilledrectangle($idest, 0, 0, $width, $height, $transparent);
imagecolortransparent($idest, $transparent);
}
imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
getResultImage($idest, $dest, $size['mime']);
imagedestroy($isrc);
imagedestroy($idest);
return true;
}
function getResultImage($dst_r, $dest_path, $type)
{
switch ($type) {
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
return imagejpeg($dst_r, $dest_path, 90);
break;
case 'image/png';
return imagepng($dst_r, $dest_path, 2);
break;
case 'image/gif';
return imagegif($dst_r, $dest_path);
break;
default:
return;
}
}