How to change the shape of an image using PHP and imagemagick? - php

I need to manipulate images on the server-side via PHP. I use ImageMagick for that purpose, and there are a lot of great functions, but no documentation...
The issue is the next:
I have two images a png with a transparent background, and a jpg one;
I need to change the JPG image that has the same shape as the visible part of the PNG.
I don't want to crop the image, but to adjust to the PNG's visible part. Is that possible to do?
Example:

Related

Fastest way to extract a crop from a JPEG file on demand?

Users on my website can click a portion of a photo to get a closeup of it.
I currently have a bit of JavaScript that fires off an AJAX call to a PHP script that uses ImageMagick to retrieve the relevant crop.
Could I be doing this better, outside of PHP? Using ImageMagick directly somehow, or something else?
Currently the files reside on the same server as the main website, but due to space restraints I'm in the process of moving them to a separate server, so will need to make a call between the two somehow.
The photos vary in size, some are 1600x1200 and only 200KB, others are 24MP+ 20MB+ originals.
Using ImageMagick, you have two options:
Crop the image while loading it. What is then loaded initially is a cropped image.
Load the image, then crop it. What is initially loaded is the complete image.
The first method is more efficient and faster.
This method is to append the image area information to the input image(s) in square brackets ([...]) like this:
convert wizard:[130x150+80+80] -resize 200% wiz-head.png
This will crop a piece of 130x150 pixels with an offset of 80 pixels from the top left corner of the original, built-in wizard: image. Here are both images side by side, wizard: (left) and the cropped section, resized by 200%:
If you wanted to crop a JPEG, you'd use something like:
convert some.jpeg[330x250+180+280] -resize 300% output.png

How to resize gif with standard php functions without losing the animation

I have a picture uploading PHP script, which handles png, jpg and jpeg images well. The problem is, that since I am using the combo of imagealphablending, imagesavealpha, imagecreatetruecolor and imagecopyresampled to resize the picture, the gif animation is lost. I am using imagecreatefromgif to load the gif into memory.
I know one can use ImageMagick to achieve the desired result, but I wonder whether there is a solution with a combo of standard php functions to resize the gif without losing the animation. I there such a solution, or should I start to use a library?
It is possible to resize an animated GIF using GD (what you call "standard PHP").
You need to split the image into individual frames, resize them and then put everything back together.
You can see a more detailed explanation here.
However, I would really suggest you to use imagick, since it's easy to install and much easier to use for advanced tasks like this.
EDIT This is the relevant part from the answer I linked above:
If you don't have ImageMagick access, you should be able to use a
combination of the following steps to resize an animated gif (assuming
you have GD access):
Detect if the image is an animated gif: https://stackoverflow.com/questions/280658/can-i-detect-animated-gifs-using-php-and-gd
(top answer)
Split the animated gif into individual frames: [http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html][2]
Resize the individual frames: [http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/][3]
Recomposite the frames into an animated gif again: [http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html][4]

How can I add a background to my png image?

I have a default image that I want my users when they first create their profile to have, and I need to each one of them have a different background color but with the same base shape that will be in a png file, I can do this by creating for each background color create a png but I dont think it might be the best choice. How can I do this?
Use Imagick extension to work on your image. You can set a background color for your transparent png image by simply doing this
shell_exec("convert testimage.png -fill '#CCCCCC00' -opaque none image_wth_bgcolor.png");
This will give a slight gray(#CCCCCC) background to the the image.
See this link.
My suggestion is to create a png file for each colour (which seems to be the only way)
and use PHP to randomly pick one of the images.

PHP image resizing and saving as png with transparent background

I'm working on a script for which I need to do the following:
User uploads an image (JPG, PNG, GIF). PHP resizes it, increases the canvas size, and saves the images as a PNG file on the server.
Now I found this tutorial to learn how to do that:
http://www.webxpert.ro/andrei/2009/01/08/thumbnail-generation-with-php-tutorial/
However, this will create a PNG file with a white background. Is it possible to have PHP generate it with a transparent background?
Thanks!
The tutorial breaks it down into basic steps. Just find the step that puts a background colour in there, and put a transparent colour instead. You will need imagecolorallocatealpha and an alpha value of 127.

Upload three versions of an image, full size, thumbnail and resized

So far, I've been uploading one image by hand (FTP to the server when live, locally moving a file), and then resizing them on the fly using the img tag's width and height properties to resize them. Well, the images don't look good, because I need a square, cropped 100px version as a thumbnail, and then a 800px wide version for the view image page, and then finally the full-size original image for HD viewing, but I also need to apply a watermark, but only to the full res version. And I need help with an image upload script. Any sort of file upload, really. I've looked at tutorials, and they don't seem to make too much sense. Furthermore, I need to drop all three versions into a database row (which I think I can figure out). I know that I need to use something like $_FILE to do it, but I'm just really confused to the actual usage and the cropping/resizing/watermarking part really has me stumped. Solutions, anyone?
File uploading
Upload using a simple HTML form and use PHP to manipulate the image. Example
Read all the images stored in a directory and convert them in a batch. Example
Image re-sizing
Use ImageMagick or GD library to resize images. Example
Use any of the same libraries to watermark the images. Example
In all, you need to combine all these in order to upload, resize and watermark all in one go.
For the resizing, watermark etc, you should look at ImageMagic

Categories