I want to place an image over another image using php. I have two images. One is a photo. And other one is a image, of fixed size, full white. We can call this as frame. So what i need is place the photo in the middle of the frame (white image) and then save it. Can anyone pls help me?
to do this kind of work, you need to use GD library or ImageMagic
this code work with GD library
$photo_to_paste="image_to_paste.jpg"; //image 321 x 400
$white_image="white_image.jpg"; //873 x 622
$im = imagecreatefromjpeg($white_image);
$condicion = GetImageSize($photo_to_paste); // image format?
if($condicion[2] == 1) //gif
$im2 = imagecreatefromgif("$photo_to_paste");
if($condicion[2] == 2) //jpg
$im2 = imagecreatefromjpeg("$photo_to_paste");
if($condicion[2] == 3) //png
$im2 = imagecreatefrompng("$photo_to_paste");
imagecopy($im, $im2, (imagesx($im)/2)-(imagesx($im2)/2), (imagesy($im)/2)-(imagesy($im2)/2), 0, 0, imagesx($im2), imagesy($im2));
imagejpeg($im,"test4.jpg",90);
imagedestroy($im);
imagedestroy($im2);
that code will output:
Related
I'm trying to crop then resize an image on PHP v5.4, I've read these resources
Put PNG over a JPG in PHP
http://php.net/manual/en/function.imagecopy.php
http://php.net/manual/en/function.imagecopyresampled.php
http://php.net/manual/en/function.call-user-func-array.phPP
PHP watermarking
http://php.net/manual/en/function.imagecreatetruecolor.php
My code is based off the answer from Cropping image in PHP (the dimensions between these images vary alot).
I want to resize this image from 1151x768 to 200x82 and crop the background section at x: 0, y: 686
I'd prefer not bloating the question with the entire 600 lines in this question, $output refers to setwidth1200nzpioneerthursday08398 image
<?php
$output = imagecreatefromjpeg("setwidth1200nzpioneerthursday08398.jpg");
$source_crop_image = imagecreatetruecolor(200, 82);
if(!is_resource($source_crop_image)) {
return $source_crop_image;
}
imagealphablending($output, true);
$source_copy_result = imagecopy($output, $source_crop_image, 0, 0, 0, 686, 200, 82);
$source_copy_result = (bool) $source_copy_result;
if(!$source_copy_result) {
return false;
}
$source_image_result = imagejpeg($source_crop_image, "images/mynewimage.jpg");
$source_image_result = (bool) $source_image_result;
?>
My Image setwidth1200nzpioneerthursday08398
Ideally I'm trying to get it crop the RED SECTION, while keeping the scale intact then resizing to 200x82
My Result
My Expected Result (I created this image using GIMP).
I have no idea why my resulting image is a black box ..
You have imagecopy() arguments in wrong order.
The right one is $source_copy_result = imagecopy($source_crop_image, $output, 0, 0, 0, 686, 200, 82);
I need to crop the image with PHP by using the dimensions.
And save it into the local with JPEG format.
Dimensions that i receive is,
{"left":82.5,"top":48.875,"width":660,"height":371.25}
I need to crop from Original size of the image.
Ex. image is 1200x800, then the result image dimension from the actual size, not resizing or any. Because the quality should be same.
How could i use these params to crop the image ?
Is it possible ?
Use the built-in imagick class:
$image = realpath("/path/to/your/image.extension");
$cropped = realpath("/path/to/your/output/image.png");
$imObj = new Imagick();
$imObj->cropImage($width, $height, $offset_x, $offset_y);
$imObj->setImageFormat("png"); // this is unnesesary, you can force an image format with the extension of the output filename.
$imObj->writeImage($cropped);
As for lossless output, use an image format with lossless encoding. PNG is perfect for the job, since it was designed for network transfer (hence the "Adam-7" interlacing).
Check this related question about lossless image formats on graphic design stack:
What are lossless image formats?
You can use imageCopyResampled function which was designed pretty much exactly for this.
$image = imagecreatefromjpeg($imageFileURL);
/***
* resize values (imported)
***/
$left = 82;
$top = 49;
$width = 660;
$height = 371;
/***
* Create destination image
***/
$newImage = imagecreatetruecolor($width,$height);
$saveToFile = "destintion filespace of image file.jpg"
if(imagecopyresampled($newImage, $image, //dest/source images
0, 0, // dest coordinates
$left, $top, // source coordinates
$width, $height, // size of area to paste to
$width, $height // size of area to copy from
)){
imagejpeg($newImage,$saveToFile,100); //zero compression saved to file
print "image resized ok!!";
}
The new fileimage will be the size specified with $width,$height and will be offset from the original image by the values given in $left and $top. From your question this looks like what you want. This will not resize or change the compression of the image (until you save the file and then possibly set these details yourself).
I am not able to crop the image bellow properly using this code. I am not sure about the problem but some of the jpg are cropped correctly with this code.
For coordinate selection i am using Image Cropper in front end.
For the image i have attached bellow, cropping doesn't happen according to starting point x and y, and it starts from different position for some images like the one bellow.
Cropping width and height is ok but starting point is not though i have provided x and y value.
$sourceFile = 'test.jpg';
$destinationFile = 'dest/test.jpg';
$jsonData = '{"x": 142, "y":233, "width":372, "height":209}';
$dataCropValue = json_decode($jsonData);
$src_img = imagecreatefromjpeg($sourceFile);
list($width, $height) = getimagesize($sourceFile);
$dst_img = imagecreatetruecolor($dataCropValue->width, $dataCropValue->height);
imagecopyresampled($dst_img, $src_img, 0, 0 , $dataCropValue->x , $dataCropValue->y ,
$dataCropValue->width, $dataCropValue->height ,
$dataCropValue->width, $dataCropValue->height);
imagejpeg($dst_img, $destinationFile);
I'm currently using imagemagick version ImageMagick 6.8.4-6 2013-04-04 Q16
with Imagick extension version 1620
I am trying to rotate a jpg image and merge this into another jpg image however when i merge the image i get a black box arround the image.
Please see the code i am using below:
public function image($images,$x,$y,$angle){
if($images != "" && $images != NULL){
$base = $this->instance;
$layer = new Imagick($images);
//resize image
if($this->id == 45){
$layer->scaleImage(329,0);
}
if($this->id == 44){
$layer->scaleImage(280,0);
}
if($this->id == 42){
$layer->scaleImage(350,0);
}
//rotate image
$layer->rotateImage(new ImagickPixel("none"), $angle);
//Merge Image
if($this->id == 44){
$base->compositeImage($layer, imagick::COMPOSITE_OVER, $x, $y);
}else{
$base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, $x, $y);
}
$this->image = $base;
}
}
The the test is currently been run when $this->id uses 44.
Can anyone shed light on this issue?
Thanks in advance
You need to use imagecolortransparent in order to have transparency capabilities.
N.B. JPG does not have transparent properties, only PNG and GIF files (and TIFF) but browsers do not support that format.
Your output file will need to be converted to one of those formats, preferably PNG then set the transparency for the desired color.
"so would you suggest converting to PNG then rotating? then merging the PNG image into the JPG (if it is possible) as the image output is required to be a jpg"
You will lose transparency as soon as you resave as JPG
I came across this via Google, here's the correct answer as others may find it. You need to use setImageMatte(1) to enable the transparency, e.g.
$src->setImageMatte(1);
$mask->rotateImage(new ImagickPixel('#00000000'), 10);
You may also use an image Mask, where black will become transparent using
$src->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
Thanks for taking the time to read my problem:
I'm using the following code to get an image and then change the color 201,2,255 (r,g,b) - which is a shade of purple then output the image.
$imgname = "input.gif";
$im = imagecreatefromgif ($imgname);
$index = imagecolorclosest ( $im, 201,2,255 ); // get White COlor
imagecolorset($im,$index,60,140,48); // SET NEW COLOR
$imgname = "output.gif";
imagegif($im, $imgname ); // save image as gif
imagedestroy($im);
This works perfectly which can be seen here : http://www.office-desks.co.uk/cache_images/test.php (top 2 images)
The problem is when I try todo exactly the same but using a jpeg instead it doesn't work.. (bottom 2 images)
$imgname = "input.jpg";
$im = imagecreatefromjpeg ($imgname);
$index = imagecolorclosest ( $im, 201,2,255 ); // get pink/purple COlor
imagecolorset($im,$index,60,140,48); // SET NEW DECENT COLOR
$imgname = "output.jpg";
imagejpeg($im, $imgname ); // save image as gif
imagedestroy($im);
If anyone could help me shed some light on the problem, would be much appreciated. Thanks in advance all.
A bit late, but I think that has to do with the 'artifacts' (compression errors) generated by JPG. Zoom in on a JPG and you see 'grains' of pixels that won't match the neighbors. This means big planes of a single color will become many colors and not 1 single value.
Stick with lossless image formats like gif or png.