educational

Image Manipulation Using PHP

I bet that many of you who develop websites have had this problem at least one time: how to resize, rotate and manipulate pics using PHP. I myself had this problem some time ago, and so I searched for the best solution. Among the options I found was a tool called ImageMagick.

ImageMagick is a standalone, command-line program that can be called with parameters to perform a requested change to either a single image, or to a set of images. ImageMagick is by far more complex than any other image processing tool I found, and although there may be other tools, I can recommend this one.

Since all of your image processing can be easily done by this tool, all you have to do is to install it on your Web server and then call it with the proper parameters. If you are using a Linux Web server, there are some configuration issues that you have to take care of, such as specifying full paths (paths from the root folder, not relative paths), and setting ‘umask’ to "0" in order to prevent changing file permissions in a way that will cause you problems in the future.

Here are some examples of how you can use ImageMagick:

• To Make Thumbnails:

<?
// Set the path to where the ImageMagick program is on your server
$path_imagemagick="/usr/X11R6/bin/";

// Set the path and filename to the picture
$path_picture="/www/sitename/pictures/picture1.jpg";

// Set the path and filename to where the thumb will be created
$path_thumb="/www/sitename/thumbs/thumb1.jpg";

// Set the largest dimension of the thumbnail (width for landscape and height for portraits)
$thumbnail_size=120;

// Set umask to 0 in order not to affect the file permissions during the next operation
umask(000);

// Execute the shell command that calls Image Magick with the proper parameters (as set above)
$ok=shell_exec($path_imagemagick."convert -size $thumbnail_size $path_picture -geometry $thumbnail_size $path_thumb");

// If shell_exec above returns 1 this mean that the operation was sucessful.
if ($ok)
{
echo "Thumbnail Created OK";
}

?>

• To Resize Images:
This is done the same way as above, except that the $path_thumb (which is the destination file) will have the same value as $path_picture – and this way the script will resize the original image, rather than create a new image.

• To Rotate Images:
To rotate an image, you must decide how much to rotate it (the number of degrees), and in which direction. Actually, all you have to know is the number of degrees you wish to rotate, since the direction is indicated by the plus and minus sign. So, +90 degrees means to rotate the pic counter clockwise for a quarter of circle (full circle is 360 degrees); -90 meens to rotate it clockwise for a quarter of circle:

<?
// Set the path to where the ImageMagick program is on your server
$path_imagemagick="/usr/X11R6/bin/";

// Set the path and filename to the picture
$path_picture="/www/sitename/pictures/picture1.jpg";

// Set the degrees with whom the picture will be rotated. Positive means counter clockwise and negative clockwise.
$degrees=-90;

// Set umask to 0 in order not to affect the file permissions with the next operation
umask(000);
$ok=shell_exec ($path_imagemagick."convert -rotate $degrees $path_picture $path_picture");

// If shell_exec above returns 1 this mean that the operation was sucessful.
if ($ok)
{
echo "Picture Rotated OK";
}

?>

• Apply A Watermark:

To do this, you will need to have an aditional image that will be used for the watermark.

<?
// Set the path to where the ImageMagick program is on your server
$path_imagemagick="/usr/X11R6/bin/";

// Set the path and filename to the picture
$path_picture="/www/sitename/pictures/picture1.jpg";

// Set the path and filename to the watermark picture
$path_watermark="/www/sitename/images/watermark.jpg";

// Set umask to 0 in order to not affect the file permissions during the next operation
umask(000);
$ok=shell_exec ($path_imagemagick."composite -watermark 15x70 -gravity SouthEast $path_watermark $path_picture
$path_picture");

// If shell_exec above returns 1 this mean that the operation was sucessful.
if ($ok)
{
echo "Picture Watermarked OK";
}

?>

As you can see, ImageMagick is a great tool for dynamically processing images using a PHP-enabled Web server. Try it for yourself, and see how easy automating your image manipulation tasks can be!

Copyright © 2024 Adnet Media. All Rights Reserved. XBIZ is a trademark of Adnet Media.
Reproduction in whole or in part in any form or medium without express written permission is prohibited.

More Articles

profile

'Traffic Captain' Andy Wullmer Braves the High Seas as Spirited Exec

Wullmer networked and hobnobbed, gaining expertise in everything from ecommerce to SEO and traffic, making connections and over time rising through the ranks of several companies to become CEO of the mobile business arm of TrafficPartner.

Alejandro Freixes ·
opinion

To Cloud or Not to Cloud, That Is the Question

Let’s be honest. It just sounds way cooler to say your business is “in the cloud,” right? Buzzwords make everything sound chic and relevant. In fact, someone uninformed might even assume that any hosting that is not in the cloud is inferior. So what’s the truth?

Brad Mitchell ·
opinion

Upcoming Visa Price Changes to Registration, Transaction Fees

Visa is updating its fee structure. Effective April 1, both the card brand’s initial nonrefundable application fee and annual renewal fee will increase from $500 to $950. Visa is also introducing a fee of 10 cents for each settled transaction, and 10 basis points — 0.1% — on the payment volume of certain merchant accounts.

Jonathan Corona ·
opinion

Unpacking the New Digital Services Act

Do you hear the word “regulation” and get nervous? When it comes to the EU’s Digital Services Act (DSA), you shouldn’t worry. If you’re complying with the most up-to-date card brand regulations, you can breathe a sigh of relief.

Cathy Beardsley ·
opinion

The Perils of Relying on ChatGPT for Legal Advice

It surprised me how many people admitted that they had used ChatGPT or similar services either to draft legal documents or to provide legal advice. “Surprised” is probably an understatement of my reaction to learning about this, as “horrified” more accurately describes my emotional response.

Corey D. Silverstein ·
profile

WIA Profile: Holly Randall

If you’re one of the many regular listeners to Holly Randall’s celebrated podcast, you are already familiar with her charming intro spiel: “Hi, I’m Holly Randall and welcome to my podcast, ‘Holly Randall Unfiltered.’ This is the show about sex, the adult industry and the people in it.

Women In Adult ·
trends

What's Hot Now: Leading Content Players on Trending Genres, Monetization Strategies

The juggernaut creator economy hurtles along, fueled by ever-ascendant demand for personality-based authenticity and intimacy — yet any reports of the demise of the traditional paysite are greatly exaggerated.

Alejandro Freixes ·
opinion

An Ethical Approach to Global Tech Staffing

One thing my 24-year career as a technologist working to support the online adult entertainment industry has taught me about is the power of global staffing. Without a doubt, I have achieved significantly more business success as a direct result of hiring abroad.

Brad Mitchell ·
opinion

Finding the Right Payment Partner

Whenever I am talking with businesses that are just getting started, one particular question comes up a lot: “How do I get a merchant account?” It’s a simple question, but it has a complicated answer.

Jonathan Corona ·
opinion

The Taxman Cometh for Every Business

February may be the month of romance, but it is also a time when we need to think about something that inspires very little love: taxes. April is not far away, and the taxman is always waiting. This year, federal and most state income taxes are due Monday, April 15.

Cathy Beardsley ·
Show More