August 6, 2011

PHP Image Manipulation and Related Issues

PHP Image Manipulation and Related Issues

Image manipulation in php is always remain as favorite as upload image functionality being better. Multiple uploader like fancy uploader using mootools lighten the image manipulation aspect once again. HTML 5 is coming up with the feature of progress bar and multiple uploads. This will certainly open another door towards image manipulation.

The interesting part of image manipulation is that when you are going to manipulate an image so image can be of any time and you have to maintain its quality and better size for web. Many forums and blogs are filled with a lot of scripts where one example is resolving one issue so the other is resolving second. You can never be ended with a final image manipulation library holding all the required functionality in it.

Codeigniter is one of mostly used php framework with img_lib.php in libraries folder but it still covering this gap that what any of images can be manipulated.

Following issues arise when you do image manipulation:

  1. Source image or uploaded image can be of any type and if you want it to manipulate
    e.g.
    i) Resize
    ii) Rotate etc.
    So you have to maintain its quality and suitable file size of web world.
  2. Filesize can create problem specially when you are on shared server with less RAM and other limited resources.

  3. Sometimes you have to maintain aspect ratio of an image while resizing but those created thumbnails are not looking good on the website because uploaded image was PNG. And resized one is available with black background.
  4. Codeigniter provides transparency options for watermarking, in which you can give the margin good transparency.
  5. Sometimes you need to write text on image so you have to upload TTF fonts in other required font extensions for web. It is good about PHP functions that these work fine if parameters are correctly passed and logic is written in good way.
  6. One thing should be clear to the php developers about php image manipulation and related issues that the problems are always there and the suggested or recommended solutions will not be waiting for you on google or any other forums. You have to think about it and solve it, yeah! you might be lucky if you found something ready made but not so often. I am going to discuss about some objectives which might will you face in future either for some sort of facebook applications or crystal pieces products.

Expected Obstacles:

  1. You might will require to resize a large image into multiple sizes with proportions or without, depends upon the requirements.
  2. You might will require to convert source image to a uniform image type because your web application is coded to accept a certain kind of images for better results.
  3. PNG type images can create problem while you resize or perform some sort of tweaking,
    e.g.
    i) Rotate
    ii) Flip
    iii) Transparency
    iv) Skew
    v) Merge

There is no straight forward way to do this all, there are a no. of functions which will serve you in combination. It depends upon your R&D skills or understanding of the problems that how do you understand and solve them.

e.g.
imagecopy()
&
imagecopymerge()

both can be used to merge a couple of images but experienced php developers manage with any of them. They use other image functions in combination and get the desired output.

What if you have to perform php image manipulation not on two but 3 or 8 images at the same time to get a resultant image of a no. of images?

There are two ways to do it, first one is to couple 1st and 2nd then resultant and the 3rd one till the last one by saving each output and accessing them. The other way which i will recommend is that keep your image in process and don’t render its output because image is nothing except coordinates and some other meta information but if you will render it, it will consume a lot of memory. You might will go out of disk space with in hours. As i experienced it that hard disk drive being filled with generated manipulated output of images.

Another interesting thing you can do that get first image from extension type 1 and suppose you have 2nd image with extension type 2. If you have to merge both so might convert 1st into 2nd or 2nd into 1st type then merge.

OR
Just merge both images and get the desired output.
OR
Merge both images and then convert that image which is still in memory before generating output by using
imagepng(), imagejpeg(), imagewbmp(), imagegif()

So final image will be in desired format regardless of source image or images.

I will keep writing on php image manipulation and related issues and functions which i tried and found useful for an application which can obviously compete the functionality of flash or flex but more lighter than those dependencies.

Last updated: March 19, 2014