How to Combine/merge two image using PHP imagecopy() or imagecopymerge()? - php

Please help to combine three jpg images using php image processing functions imagecopy, imagecopymerge
image 1- b.jpg
image 2- l.jpg
image3: r.jpg
output look like
I tried some example codes of the PHP functions imagecopy() or imagecopymerge()
but not working with my images.

Could you give more information about: "not working"
I would suggest you to take a look at: imagecopyresampled
For the code, you have to load the images as image ressources, so you can use something like:
$im1 = imagecreatefrompng($path.'image1.png');
$im2 = imagecreatefrompng($path.'image2.png');
imagecopyresampled($im1,$im2,250,150,0,0,100,150,100,150);
unset($im2);
$im3 = imagecreatefrompng($path.'image3.png');
imagecopyresampled($im1,$im3,550,150,0,0,100,150,100,150);
unset($im3);
So $im1 now have the 3 images.
Just make sure to unset the image ressources, because you will run out of memory really fast.
Edit: I used imagecopyresampled because image 2-3 seem smaller on the last image, I don't know how more resource intensive it is, I guess imagecopymerge could work too.

Related

imagecopyresampled results to black square image, but imagescale is fine

I have a straightforward PHP script (php version 7+) that I run to resize images to a smaller dimension around 1200px wide ...
I use imagecreatefromjpeg and imagecopyresampled method and the save new file with imagejpeg # 88% compression.
I and ran over 100 images through it and most images worked out great, except for a few stubborn images that results to BLACK square image.
I tried imagecopyresized and played with different new dimensions but it just didnt work. I even tried outputting the ob stream from these tests at the point of resizing instead of saving to file (no compressions applied) and i can confirm that the moment the image was resized, it is already black square.
On the images that produces black square the result is false.
I did verify mime type IMAGETYPE_JPEG of file, regardless of file extension, before running the code.
Now, I tried running the same files thru imagescale instead
$new_img = imagescale($img, $new_width, $new_height, IMG_SINC );
and that worked.
Now I prefer to use the previous method for better more crispier output so I've been trying to get it to work :(
Why is this happening? I searched stack for similar issues but they are describing a issues that completely didnt work on any images, not my case, where only a select few images had issues.
I am dealing with JPG only, but is it possible that the jpg file is just named with extension ".jpg" but actually a png, bmp, or something else? Will that cause issues?
I'm stuck, and i hope you guys can help.
Thank you
Most images resized nicely

Resize a PHP GD-generated image in PHP and display it

I am making an avatar script from scratch and am having some problems. I got transparency working, and multi-image support for heads, bodies, shirts, etc.
Anyhow, I want to be able to generate specific sizes of the avatar within the PHP script. At this time, I have the variable $baseImage, which is an image generated using the GD script below:
$baseImage = imagecreatefrompng($startAsset);
imagealphablending($baseImage, true);
imagesavealpha($baseImage, true);
... combine all images into $base here
header("Content-type: image/png");
imagepng($baseImage);
The size of the image this generates is 350x550 (pixels) and I want to be able to get a smaller size.
I've done research but cannot find a working solution. What built-in PHP GD functions can resize this, retain transparency, and keep the great quality/colors?
There is no way to change the size of an image resource directly. Instead, you need to create a new image of the desired size and use imagecopyresampled to copy from the fullsize image to the resized one.

PHP: how to create an image from another PNG image

I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp

How do I auto resize user's inputted images to a specific dimension in PHP?

When users input their images, their images are any size. I want to be able to resize all the images to a specific dimension. Is there a function that allows me to do that in PHP?
thanks
The function you need is imagecopyresampled, that also interpolate pixels, (imagecopyresized does not);
In my code I use a it in a function like this:
function resizeAndSavePhoto($original, $destination, $dest_width, $dest_height){
$photo = createImage($original);
$size = getimagesize($original);
$final_photo = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($final_photo, $photo,0,0,0,0,$dest_width, $dest_height, $size[0], $size[1]);
imagejpeg($final_photo, $destination, 100);
}
$orignal and $destination are filename paths
http://php.net/gd
Specifically, http://php.net/imagecopyresized
Try ImageMagick, it keeps the EXIF information in an image if it needs it, among other things:
http://php.net/manual/en/book.imagick.php
ok you can make something:
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
or use something already made, i use this one:
http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
which works a treat
Here's a very good library that you can resize images with. It has ways to resize them so they are still proportionate and it has options to save, or display images as well. Works really slick
http://phpthumb.gxdlabs.com/

How to compress images in CodeIgniter, yet do not change their dimensions?

I have a site where users can upload images. I process these images directly and resize them into 5 additional formats using the CodeIgniter Image Manipulation class. I do this quite efficiently as follow:
I always resize from the previous format, instead of from the original
I resize using an image quality of 90% which about halves the file size of jpegs
The above way of doing things I implemented after advise I got from another question I asked. My test case is a 1.6MB JPEG in RGB mode with a high resolution of 3872 x 2592. For that image, which is kind of borderline case, the resize process in total takes about 2 secs, which is acceptable to me.
Now, only one challenge remains. I want the original file to be compressed using that 90% quality but without resizing it. The idea being that that file too will take half the file size. I figured I could simply resize it to its' current dimensions, but that doesn't seem to do anything to the file or its size. Here's my code, somewhat simplified:
$sourceimage = "test.jpg";
$resize_settings['image_library'] = 'gd2';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = '90%';
$this->load->library('image_lib', $resize_settings);
$resize_settings['width'] = $imagefile['width'];
$resize_settings['height'] = $imagefile['height'];
$resize_settings['new_image'] = $filename;
$this->image_lib->initialize($resize_settings);
$this->image_lib->resize();
The above code works fine for all formats except the original. I tried debugging into the CI class to see why nothing happens and I noticed that the script detects that the dimensions did not change. Next, it simply makes a copy of that file without processing it at all. I commented that piece of code to force it to resize but now still nothing happens.
Does anybody know how to compress an image (any image, not just jpegs) to 90% using the CI class without changing the dimensions?
I guess you could do something like this:
$original_size = getimagesize('/path/to/original.jpg');
And then set the following options like this:
$resize_settings['width'] = $original_size[0];
$resize_settings['height'] = $original_size[1];
Ok, so that doesn't work due to CI trying to be smart, the way I see it you've three possible options:
Rotate the Image by 360ยบ
Watermark the Image (with a 1x1 Transparent Image)
Do It Yourself
The DIY approach is really simple, I know you don't want to use "custom" functions but take a look:
ImageJPEG(ImageCreateFromString(file_get_contents('/path/to/original.jpg')), '/where/to/save/optimized.jpg', 90);
As you can see, it's even more simpler than using CI.
PS: The snippet above can open any type of image (GIF, PNG and JPEG) and it always saves the image as JPEG with 90% of quality, I believe this is what you're trying to archive.

Categories