Related
Actually, Here user can upload any size image to fit into any frame which i have. I want to fit that image into a frame without losing ratio.
Example:
This image is not fitting with my php code.
My code is
list($width, $height) = getimagesize($img1);
$outputImage = imagecreatetruecolor($width, $height);
// set background to white
//$white = imagecolorallocate($outputImage, 255, 255, 255);
//imagefill($outputImage, 0, 0, $white);
list($width1, $height1) = getimagesize($img2);
$info1 = getimagesize($img1);
$info2 = getimagesize($img2);
$extension1 = image_type_to_extension($info1[2]);
$extension2 = image_type_to_extension($info2[2]);
if($extension1=='.jpeg') {
$first = imagecreatefromjpeg($img1);
} else {
$first = imagecreatefrompng($img1);
}
if($extension2=='.jpeg') {
$second = imagecreatefromjpeg($img2);
} else {
$second = imagecreatefrompng($img2);
}
$ratio = $width1 / $height1;
$targetWidth = 300 * $ratio;
//echo $targetWidth;
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresampled($outputImage,$first,0,0,0,0, $width, $height, $width, $height);
imagecopyresampled($outputImage,$second,110,75,0,0, $targetWidth, 300, $width1, $height1);
Please help me. Thanks in advance.
to achieve that you must detect the frame image and source image dimensions
re size the source image to fit the frame image dimensions.
note that to avoid ratio problems we will detect if the source image wide image or tall image (landscape or portrait)
if width > height then set the width to the width of frame and calculate the new height based on main ratio
if height > width
set height to frame height and calculate the new width
this is sample implementation code
<?
$src = imagecreatefromjpeg('source.jpg');//source image path
$frame = imagecreatefrompng('frame.png');//frame image
$sourceWidth = imagesx($src); // source image width
$sourceHeight = imagesy($src); // image image height
$frameWidth = imagesx($frame);//get frame width
$frameHeight = imagesy($frame);//get frame height
$NewImage = imagecreatetruecolor($frameWidth,$frameHeight);//the new image
$bg_color = imagecolorallocate ($NewImage, 255, 255, 255);
imagefill($NewImage, 0, 0, $bg_color);//set background to white
if($sourceHeight >= $sourceWidth){
$newHeight=$frameHeight;// set height to frame height
$newWidth=intval($frameWidth*$sourceWidth/$sourceHeight);//calculate the new width
imagecopyresampled($NewImage, $src, intval(($frameWidth-$newWidth)/2),0,0,0,$newWidth, $newHeight, $sourceWidth, $sourceHeight);//insert source image to new image at the center
} else {
$newWidth=$frameWidth;;// set width to frame width
$newHeight=intval($frameHeight*$sourceHeight/$sourceWidth);//calculate the new height
imagecopyresampled($NewImage, $src,0,intval(($frameHeight-$newHeight)/2),0,0,$newWidth, $newHeight,$sourceWidth, $sourceHeight);//insert source image to new image at the center
}
imagecopyresampled($NewImage, $frame, 0,0, 0,0, $frameWidth, $frameHeight, $frameWidth, $frameHeight);//copy frame imageto new image
header("Content-type: image/png");
imagejpeg($NewImage);
?>
hope this would help.
I'm using Jquery plugin ImgAreaSelect with PHP 5.3.9 with GD 2.0.34.
Following some examples from the plugin, I added a form that gives X and Y values from where I start selecting the image till the end of the selection.
This is going OK, because I recieve the values correctly, but I cannot crop the image. Followed some examples/tutorials but always failed.
Here is my PHP code:
$x1 = $_POST['x1']; //this one gives me the point where start to crop
$x2 = $_POST['x2']; //the end of X axis
$y1 = $_POST['y1']; //same for Y1 and Y2
$y2 = $_POST['y2'];
$w = $x2 - $x1; //getting the width for the new image
$h = $y2 - $y1; //getting the height for the new image
$src_img = "path/image";
$format = end(explode(".", $src_img)); //taking the image format (jpg, png, gif)
$size = getimagesize($src_img);
switch($format) {
case "jpg":
$copy = imagecreatefromjpeg($src_img);
$new = ImageCreateTrueColor($w, $h);
imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $size[0], $size[1]);
header('Content-type: image/jpeg');
imagejpeg($new);
break;
}
I would like to know if there's something wrong (most probably).
Thanks for all and taking your time to help.
imagecopyresampled($new, $copy, $x1, $y1, 0, 0, $w, $h, $size[0], $size[1]);
http://php.net/manual/en/function.imagecopyresampled.php
In other words, imagecopyresampled() will take a rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).
in other words, you need to change it to:
imagecopyresampled($new, $copy,0 ,0 ,$x1, $y1, $w, $h, $w, $h);
Anyway you can try this code also
<?php
$x1 = $_POST['x1']; //this one gives me the point where start to crop
$x2 = $_POST['x2']; //the end of X axis
$y1 = $_POST['y1']; //same for Y1 and Y2
$y2 = $_POST['y2'];
$w = ( $x2 - $x1 ); //getting the width for the new image
$h = ( $y2 - $y1 ); //getting the height for the new image
$src = "path_to_file";
$info = getimagesize( $src );
switch( $info[2] ) {
case IMAGETYPE_JPEG:
$copy = imagecreatefromjpeg( $src );
$new = imagecreatetruecolor( $w, $h );
imagecopyresampled( $new, $copy, 0, 0, $x1, $y1, $info[0], $info[1], $w, $h );
header( 'Content-type: image/jpeg' );
imagejpeg( $new );
break;
default:
break;
}
?>
//resize and crop image by center
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
$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 = 7;
break;
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$quality = 80;
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,
Resize and crop image from center with PHP
$max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");
thanks to all, #Deepanshu gave me a link wich I saw a piece of code, but somehow strange:
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
the last $src_w and $src_h I had to put the new width and the new height, instead the width and height of the original image.
so, the final code working properly is:
imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $w, $h);
I have the follow PHP code:
$w = 300; // Width of new image
$h = 300; // Height of new image
$oh = 540; // Original file height
$ow = 720; // Original file width
$x = 196;
$y = 50;
$image = imagecreatefromjpeg('fileToCrop.jpg');
$cropped_image = imagecreatetruecolor($w, $h);
imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $ow, $oh, $w, $h);
imagejpeg($cropped_image, 'fileToCrop.jpg', 100);
And want to crop the image, but my images are distorting / higher than original, eg:
Original:
Cropped ("N" for "Not" are showing):
I can't see what is wrong with my code, and what are happening to images goes bigger..
You inverted the last 4 parameters of imagecopyresampled
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
Change it for
imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $w, $h, $ow, $oh);
But in fact, are you sure you're not looking for a straight copy of the cropped region?
imagecopy($cropped_image, $image, 0, 0, $x, $y, $ow, $oh);
because you omitted the quality on imagejpeg() it defaults to 75%
http://php.net/manual/en/function.imagejpeg.php
can you try adding 95 on 3rd parameter?
imagejpeg($cropped_image, 'fileToCrop.jpg', 95);
P.S.
If you can use a 3rdparty php script I'll suggest you just use timthumb
http://www.binarymoon.co.uk/projects/timthumb/
if you need a change of cropping location,
http://www.binarymoon.co.uk/2010/08/timthumb-part-4-moving-crop-location/
i am creating a new feature on my site that allow people to send postcard to friends. in this section they can choose the image they want to send (they already uplaoded the image to their profile -> my pictures section)
i am using the php function to create the text that goes on the right but how can i add another image to this image with the text?
i use imagettftext to create the text, imagecreatefromjpeg to open the main image (see below) and imagedestroy when im done
thanks
i am using this postcard:
First you will have to crop the image to fit in your postcard.
Based on your image here's what you have to do:
<?php
$sourceImage = './postcard-template.jpg';
$uploadedImage = '/path/to/image/hong-kong2.jpg'; // let's get hong kong as example
$mime = '';
$font = '/path/to/font/arial.ttf';
function CroppedThumbnail($source, $width, $height, &$mime) {
$data = getimagesize($source);
$sourceWidth = $data[0];
$sourceHeight = $data[1];
$mime = $data['mime'];
$image = imagecreatefromjpeg($source);
$sourceRatio = $sourceWidth/$sourceHeight;
if (($width/$height) > $sourceRatio) {
$newHeight = $width/$sourceRatio;
$newWidth = $width;
}
else {
$newWidth = $height*$sourceRatio;
$newHeight = $height;
}
$croppedImage = imagecreatetruecolor(round($newWidth), round($newHeight));
imagecopyresampled($croppedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $croppedImage, 0, 0, (($newWidth/2)-($width/2)), (($newHeight/2)-($height/2)), $width, $height, $width, $height);
imagedestroy($croppedImage);
imagedestroy($image);
return $thumb;
}
// Create the cropped image first
$newThumb = CroppedThumbnail($uploadedImage,240,315, $mime);
switch($mime) {
case 'image/gif':
$image = imagecreatefromgif($sourceImage);
break;
case 'image/jpeg':
$image = imagecreatefromjpeg($sourceImage);
break;
case 'image/png':
$image = imagecreatefrompng($sourceImage);
break;
default:
// error or stop script
break;
}
$message = "this is some text\nsome other text\ntext text";
imagettftext($image, 21, 0, 320, 255, imagecolorallocate($image, 0, 0, 0), $font, $message);
imagecopy($image, $newThumb, 40, 40, 0, 0, 240, 315);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
For example I use this image ( needs to be cropped ) :
then it will output:
Use imagecopymerge to copy the photo onto the post card
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 )
everything in my code is working great for creating a thumbnail image of an uploaded picture.
now all i need to do is crop the $thumb from the center of the image into a square shape (50x50)
heres my function so far
$ext = end(explode('.', $_FILES['profile_photo']['name']));
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif')
{
$tmp = $_FILES['profile_photo']['tmp_name'];
if ($ext=='jpg' || $ext=='jpeg')
$src = imagecreatefromjpeg($tmp);
else if ($ext=='png')
$src = imagecreatefrompng($tmp);
else
$src = imagecreatefromgif($tmp);
list($width,$height) = getimagesize($tmp);
$thumb_width = 50;
$thumb_height = ($height/$width) * $thumb_width;
$thumb_tmp = imagecreatetruecolor($thumb_width, $thumb_height);
$full_width = 200;
$full_height = ($height/$width) * $full_width;
$full_tmp = imagecreatetruecolor($full_width, $full_height);
imagecopyresampled($thumb_tmp, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagecopyresampled($full_tmp, $src, 0, 0, 0, 0, $full_width, $full_height, $width, $height);
imagejpeg($thumb_tmp, 'images/profile/'.$user['id'].'_'.time().'_thumb.'.$ext, 100);
imagejpeg($full_tmp, 'images/profile/'.$user['id'].'_'.time().'_full.'.$ext, 100);
imagedestroy($src);
imagedestroy($thumb_tmp);
imagedestroy($full_tmp);
// delete old image from server if it is not none.png
}
any help would be greatly appreciated! i know that it has something to do with imagecopyresampled but i can't figure out the math for the cropping from the center of the image. i want this to be my own function so please dont recommend me using other peoples classes.
Right after $full_tmp = imagecreatetruecolor($full_width, $full_height);, add...
if ($thumb_width > $thumb_height) {
$thumb_offset = array('x' => ($thumb_width/2 - 25), 'y' => 0);
} else {
$thumb_offset = array('x' => 0, 'y' => ($thumb_height/2 - 25));
}
$square_tmp = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($square_tmp, $src, 0, 0, $thumb_offset['x'], $thumb_offset['y'], 50, 50, $width, $height);
Then save and destroy the temp like the other two images.
Take a look at the parameters that should be passed to imagecopyresampled, as per the PHP manual:
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
From the third parameter on, you basically define how a rectangle on the source image maps to a rectangle on the destination image.
So the first thing you have to do is calculate the rectanle (x, y, width and height) which defines the visible area of your original image. These will be the 5th, 6th, 9th and 10th parameters to the function, respectively.
For the destination rectangle, use 0,0 for x,y, and $thumb_width,$thumb_height for w,h, just as you are currently doing.