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);
}
Related
I have this image 0.jpg and his dimension is 800*534 and size is 44, 8 ko.
When i use this function:
function image_reduce($image, $desired_width, $target_file, $extension, $quality = 100){
ini_set('gd.jpeg_ignore_warning', 1);
$source_image = imagecreatefromjpeg($image);
if($source_image){
$width = imagesx($source_image);
$height = imagesy($source_image);
if($width > $desired_width){
$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);
imagejpeg($virtual_image, $target_file, $quality);
}
else imagejpeg($source_image, $target_file, $quality);
imagedestroy($source_image);
return false;
}
else return true;
}
to reduce the image i obtain a new image who have the dimension 600*400 but the size is 140ko.
Everybody can help me?
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
**/
}
?>
Original Image:
Desired output: (I added black border for the sack of understanding)
I want to resize image to 200/200 without cropping. See the desired output.
I have this code
<?php
// function created by www.thewebhelp.com
if (!function_exists("create_square_image")) {
function create_square_image($original_file, $destination_file = NULL, $square_size = 96) {
if (isset($destination_file) and $destination_file != NULL) {
if (!is_writable($destination_file)) {
echo '<p style="color:#FF0000">Oops, the destination path is not writable. Make that file or its parent folder wirtable.</p>';
}
}
// get width and height of original image
$imagedata = getimagesize($original_file);
$original_width = $imagedata[0];
$original_height = $imagedata[1];
if ($original_width > $original_height) {
$new_height = $square_size;
$new_width = $new_height * ($original_width / $original_height);
}
if ($original_height > $original_width) {
$new_width = $square_size;
$new_height = $new_width * ($original_height / $original_width);
}
if ($original_height == $original_width) {
$new_width = $square_size;
$new_height = $square_size;
}
$new_width = round($new_width);
$new_height = round($new_height);
// load the image
if (substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")) {
$original_image = imagecreatefromjpeg($original_file);
}
if (substr_count(strtolower($original_file), ".gif")) {
$original_image = imagecreatefromgif($original_file);
}
if (substr_count(strtolower($original_file), ".png")) {
$original_image = imagecreatefrompng($original_file);
}
$smaller_image = imagecreatetruecolor($new_width, $new_height);
$square_image = imagecreatetruecolor($square_size, $square_size);
imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if ($new_width > $new_height) {
$difference = $new_width - $new_height;
$half_difference = round($difference / 2);
imagecopyresampled($square_image, $smaller_image, 0 - $half_difference + 1, 0, 0, 0, $square_size + $difference, $square_size, $new_width, $new_height);
}
if ($new_height > $new_width) {
$difference = $new_height - $new_width;
$half_difference = round($difference / 2);
imagecopyresampled($square_image, $smaller_image, 0, 0 - $half_difference + 1, 0, 0, $square_size, $square_size + $difference, $new_width, $new_height);
}
if ($new_height == $new_width) {
imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
}
// if no destination file was given then display a png
if (!$destination_file) {
imagepng($square_image, NULL, 9);
}
// save the smaller image FILE if destination file given
if (substr_count(strtolower($destination_file), ".jpg")) {
imagejpeg($square_image, $destination_file, 100);
}
if (substr_count(strtolower($destination_file), ".gif")) {
imagegif($square_image, $destination_file);
}
if (substr_count(strtolower($destination_file), ".png")) {
imagepng($square_image, $destination_file, 9);
}
imagedestroy($original_image);
imagedestroy($smaller_image);
imagedestroy($square_image);
}
}
create_square_image("image.jpg", "sample_thumb.jpg", 200);
?>
<h2>Original image</h2>
<h2><img src="image.jpg" />
</h2>
<h2>The created square thumbnail</h2>
<h2><img src="sample_thumb.jpg" />
</h2>
But my code is outputting like this
How about using a white image that matches your dimensions? I.e you get a white image that is your desired size (200x200) and merge it with your other image.
// First image is the white image that has the dimension you want.
$image1 = imagecreatefrompng('COLOR.png');
// Second image is the image you want to change size of
$image2 = imagecreatefrompng('SOURCE.png');
// Merge the two, so that the white image is below the "Dress"
// image you showed us in your question.
imagecopymerge($image1, $image2, 0, 0, 0, 0, 161, 200, 100);
// Output and free from memory
header('Content-Type: image/png');
echo imagepng($image1);
// Destroy images
imagedestroy($image1);
imagedestroy($image2);
Parameters for the function:
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x
, int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int
$pct )
Last parameter is percentage(alpha) so if you don't want a white backround then you just set it to 0, and it will be transparent.
EDIT: Check out the docs so that you get the correct position for the second image here.
EDIT 2: The above code will produce the following image(provided the color image is black):
I have found the solution that works for both situation whether image is wide or tall.
$whereToPut = "source.jpg"; // This is 200/200 blank white image
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
$dst_y = 0;
$dst_x = 0;
if ($ratio > 1) {
$width = 200;
$height = 200 / $ratio;
$dst_y = (200 - $height) / 2;
} else {
$width = 200 * $ratio;
$height = 200;
$dst_x = (200 - $width) / 2;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
$image1 = imagecreatefromjpeg($whereToPut);
imagecopymerge($image1, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100);
imagejpeg($image1, $finalImage);
// $dinalImage is your final created image.
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);
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);