I am trying to crop an image on PHP. I saw that there is a function for doing that on PHP: imagecrop. Here is the code in which I am trying to crop the image.
$image = imagecreatefromjpeg(//Path of the image);
$croppedImage = imagecrop($image, array("x"=>0,"y"=>0,"width"=>100,"height"=>100));
imagejpeg($croppedImage, //Path in which the image will be stored);
Here I want that the crop of the image starts in the left corner of the image and get the values of width and height that I put above.
But it just resize my image, not crop it. What am I doing wrong?
EDIT:
I tried also with the function imagecopyresampled. Here it is what I have tried:
dst_image(Destination image link resource) = newImage; //Here the new image that I want to create.
src_image(Source image link resource) = image; //Here the original image.
//Here 0,0 because I want that the new image crop starts in the left corner of the original image.
dst_x(x-coordinate of destination point) = 0;
dst_y(y-coordinate of destination point) = 0;
//Here 0,0 because I want that the crop starts on the 0,0 of the original image
src_x(x-coordinate of source point) = 0;
src_y(y-coordinate of source point) = 0;
dst_w(Destination width) = 150; //The new width of the crop image.
dst_h(Destination height) = 150; //The new height of the crop image.
src_w(Source width) = 500; //The original width of the image.
src_h(Source height) = 500; //The original height of the image.
So finally the function will be like:
$b = imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w , $dst_h , $src_w , $src_h );
I just have problems in this function it is why avoid the rest (save and imagetruecolor and the rest...)
This function give to me the result expected but the new image it is black, why?
Thanks in advance!
try this:
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // $src_x + $dst_w Crop end X position in original image
$src_h = 220; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/cropped_whatever.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
Alternatively you could use the imagecrop function of imagick.
Finally, I got the solution of my problem. I had to put the same value of dst_w and src_w. The same to dst_h and src_h. ¡¡And it works!!
The final solution it is the following:
$b = imagecopyresampled ($dst_image, $src_image, 0, 0, 0, 0, 150, 150, 150, 150);
Related
I'm trying to add a watermark (logo) to each image upload to the website.
So, I used imagecopy PHP function to add a watermark (a png image) to the main image (a jpg image) but the problem is the logo size is changing according to the main image size (height and width), That's mean if I upload a 4000x2000 image the logo with be somthing like 100x100 and if the main image size is 1000x500 the stamp will be bigger than the real size (546x537).
Image Samples:
https://crkemlak.com/appimg/199f8486d7d77007771f2f450dffca4d.jpeg
https://crkemlak.com/appimg/d6f9fd02999eced76eac9a6995df904f.jpeg
https://crkemlak.com/img/stamp.png
I used this code to add the watermark to the image:
$im = imagecreatefromjpeg('../appimg/'.$filenamerand);
$originalWidth= imagesx($im);
$originalHeight = imagesy($im);
$stamp = imagecreatefrompng('../img/stamp.png');
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, ($originalWidth-$sx)/2, ($originalHeight-$sy)/2, 0, 0, imagesx($stamp), imagesy($stamp));
I need your help please to fix this problem, I need to make the watermark is in it's real size in any size of the main jpg image
Thanks
I used imagecopyresized so that the watermark could be scaled to look the same on any input image irregardless of its size. Possibly better ways to do it. Im not sure how good the quality is when scaling images with transparent backgrounds. Here it is on git
$watermark = imagecreatefrompng('watermark.png');
$image = imagecreatefromjpeg('main-image.jpg');
$wm_x = imagesx($watermark);
$wm_y = imagesy($watermark);
$img_x = imagesx($image);
$img_y = imagesy($image);
// calculate watermark size
$wm_scale = 19; // set size in relation to image
$wm_w = $img_x/$wm_scale;
$wm_aspect = $wm_y/$wm_x;
$wm_h = (int) ($wm_aspect * $wm_w);
// calculate margin
$margin_scale = 6; // set margin in relation to new watermark size
$margin_right = $wm_w/$margin_scale;
$margin_bottom = $wm_h/$margin_scale;
// calculate watermark destination
$dst_x = $img_x - $wm_w - $margin_right;
$dst_y = $img_y - $wm_h - $margin_bottom;
imagecopyresized ($image, $watermark, $dst_x, $dst_y, 0, 0, $wm_w, $wm_h, $wm_x, $wm_y);
// Output and free memory
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
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.
in my project i just do image watermarking or image combine it's working fine and code for that.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];
$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname= time();
$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
// Set the thumbnail name
$thumbnail = $folder.$newname.".".$ext;
$imgname=$newname.".".$ext;
// Load the mian image
if ($ext=="png" || $ext=="PNG") {
$source = imagecreatefrompng($uploadimage);
}
else if ($ext=="gif" || $ext=="GIF") {
$source = imagecreatefromgif($uploadimage);
}
else if ($ext=="bmp" || $ext=="BMP") {
$source = imagecreatefrombmp($uploadimage);
}
else{
$source = imagecreatefromjpeg($uploadimage);
}
// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');
// get the width and height of the watermark image
$water_width = imagesx($source)/2;
$water_height = imagesy($watermark);
// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);
$im_middle_w = $main_width/2;
$im_middle_h = $main_height/2;
// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis
$dime_x = $im_middle_w - $water_width/2;
$dime_y = $im_middle_h - $water_height/2;
// copy both the images
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);
// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
unlink($file);
}
?>
<img src='uploads/<?php echo $imgname;?>'>
</body>
</html>
but problem with setting $water_width and i want set as half of my source image. but when i have source image of less width or more width compare to $water_width it's set it like that. see image when source image width is more.
and when width is less.
so my problem is how to set $water_width as half of source image width?
by Alex your answer it's came up like this.
This will resize watermark to half-width of original image and put it in the centre:
// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');
// get the width and height of the watermark image
$water_width = imagesx($watermark);
$water_height = imagesy($watermark);
// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);
// resize watermark to half-width of the image
$new_height = round($water_height * $main_width / $water_width / 2);
$new_width = round($main_width / 2);
$new_watermark = imagecreatetruecolor($new_width, $new_height);
// keep transparent background
imagealphablending( $new_watermark, false );
imagesavealpha( $new_watermark, true );
imagecopyresampled($new_watermark, $watermark, 0, 0, 0, 0, $new_width, $new_height, $water_width, $water_height);
// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis
$dime_x = round(($main_width - $new_width)/2);
$dime_y = round(($main_height - $new_height)/2);
// copy both the images
imagecopy($source, $new_watermark, $dime_x, $dime_y, 0, 0, $new_width, $new_height);
// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
imagedestroy($source);
imagedestroy($watermark);
imagedestroy($new_watermark);
You can try imagettftext method, if you don't want any such high perfection in transparency. You can have try of this code. You have to save a font file in your directory, here I used arial.ttf.
$im = imagecreatefrompng("png.png"); //create image data
$font = 'arial.ttf'; //font file name
$randomString = "example.com"; //string need to be shown
$main_width = imagesx($im); //finding width and height
$main_height = imagesy($im);
$posx= $main_width/2; //finding center
$posy = $main_height/2;
$color = imagecolorallocate($im, 200, 200, 200); //Creating color
$size = ($main_width/25)+1; //determine size of font. +1 to avoid 0
$temp = $size*5;
$posx = $posx-$temp; //adjust to average center
imagettftext($im,$size,0, $posx, $posy, $color, $font , $randomString); //apply a text
You have to adjust posx and posy for your text position. Size also can be adjusted with some logics.
$color = imagecolorallocate($im, 0, 0, 0);= black
and $color = imagecolorallocate($im, 255, 255, 255); = white.
You have to adjust this for your required text color.
I have been trying to implement a rating display procedure to rating in form of stars as passed by GET in a php file.
Here's the code for rate.php :
$filename= "rating.png";
$rating = $_GET['rating'];
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefrompng($filename);
$dst_im = imagecreatefrompng("rating_back.png");
$src_x = '0'; // begin x
$src_y = '0'; // begin y
$src_w = $w * $rating / 5; // width
$src_h = $h; // height
$dst_x = '0'; // destination x
$dst_y = '0'; // destination y
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
header("Content-type: image/png");
imagepng($dst_im);
imagedestroy($dst_im);
$rating is a float value as per the GET request.
But when I try to implement rate.php?rating=4.3 using the following images:
"rating.png":
"rating_back.png":
I get the following result :
What could be the possible errors which are leading to an abnormal background in the final image?
Please give the possible solutions also.
Thanks in advance :)
It looks like those are 24 bit PNG images with transparency. You need to tell GD to save the transparency info too. Add:
imagesavealpha($dst_im, true);
before rendering your image
I have transparent image that I want to overlay over the entire image to give it the boarder effect. In this code it crops an existing image. It merges the two but the final over the top is not showing the alpha. How can I fix this?
<?
$dst_x = 0; // X-coordinate of destination point.
$dst_y = 0; // Y --coordinate of destination point.
$src_x = 163; // Crop Start X position in original image
$src_y = 0; // Crop Srart Y position in original image
$dst_w = 469; // Thumb width
$dst_h = 296; // Thumb height
$src_w = 469; // $src_x + $dst_w Crop end X position in original image
$src_h = 296; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions.( to merge with the original image )
$dst_image = imagecreatetruecolor($dst_w,$dst_h);
// Get original image
$src_image = imagecreatefromjpeg("http://www.ucatholic.com/wp-content/uploads/2012/10/Sallixtus-631x295.jpg");
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, "images/crop.jpg");
$src = imagecreatefrompng('http://www.EdVizenor.com/images/saintCover.png');
/// THIS LINE NOT WORKING - - // Copy and merge
imagecopymerge($dst_image, $src, 0, 0, 0, 0, 469, 296, 100);
header('Content-Type: image/png');
imagegif($dst_image);
?>
Dont use imagecopymerge, use imagecopy, then it will works normally and your watermark will be shown with alpha.
imagecopy($dst_image, $src, 0, 0, 0, 0, 469, 296);
If used the imagecopy instead of imagecopymerge you will lose control over alpha channel.
I think you could use Jaguar , if you want a real fast overlay action using the Gd extension.
see my answer Here if you want to see how
if you want a brushed border you can use Jaguar like this:
use Jaguar\Canvas,
Jaguar\Drawable\Border;
$canvas = new Canvas('your image');
$brush = new Canvas('style image');
$border = new Border(20);
$border->draw($canvas,$brush);
$canvas->save('path to save')->show();