Watermarking using too much CPU time

Posted on 2011-10-05
Medium Priority
Last Modified: 2013-12-25
On a property website I'm dynamically watermarking images using script that can be found here
This works beautifully as we don't want to permanently watermark images as these are included in several feeds to portal sites where they apply their own watermark.

However, the hosting company are now saying that this script is using up too much CPU time.

Anyone got any ideas as to how we can overcome this?
Question by:fionafenton
  • 4
  • 3
  • 2
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36921820
How about two copies of the images, one with the watermark and one without?
LVL 10

Accepted Solution

Derokorian earned 1000 total points
ID: 36922299
Personally I would save every image once its been watermarked. Then in your script you could check for the image existance. If it doesn't exist create it however if it does just output the existing image. Something like the following (note I've used my own watermark function as I dont like that class you linked lol but you could change it to use it)

// define a default watermark if none specified
define('DEFAULT_WATERMARK',   'watermark.png');

// I would prefer to do this with id's and a database lookup
// but for simplicity url might be image.php?image=picture101.jpg&wm=company_logo.png
$image = isset($_GET['image']) ? $_GET['image'] : FALSE;
if( !$image ) die('No image specified.');

$wm = isset($_GET['wm']) ? $_GET['wm'] : DEFAULT_WATERMARK;

$img_name = pathinfo($image,PATHINFO_FILENAME);
$wm_name = pathinfo($wm,PATHINFO_FILENAME);
$output_name = $img_name . '_' . $wm_name . '.png';

// Check if the watermark image already exists
if( file_exists($output_name) ) {
   $output = imagecreatefrompng($output_name);
} else {
   $output = watermark($image,$wm);

// output the image to the browser
header('Content-type: image/png');

function watermark($base,$watermark) 
   list($basew,$baseh) = getimagesize($base);
   list($wmw,$wmh) = getimagesize($watermark);
   // Get the scale for the watermark
   if( $basew > $baseh ) 
      $wmscale = ($basew/2)/$wmw;
   } else 
      $wmscale = ($baseh/2)/$wmh;
   $newwmw = $wmw*$wmscale;
   $newwmh = $wmh*$wmscale;

   // Create the watermark the right size
   $wmimg = imagecreatefrompng($watermark);
   $newwmimg = imagecreate($newwmw,$newwmh);

   // Open the image, place watermark on it
   $baseimg = imagecreatefromjpeg($base);
   $dst_x = ($basew/2) - ($newwmw/2);
   $dst_y = ($baseh/2) - ($newwmh/2);

   return $baseimg;

Open in new window


LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36922396
Derokorian, while yours is a variation of my answer, I like yours better.  The watermarked copy is made only if needed.  Of course, it's kept (which is my solution).  I also like your solution because it looks simple to implement and it will (almost certainly) solve the problem, as stated.

Now, if the problem is different than stated, this isn't solved.  If the watermark is supposed to contain information about the recipient, we still have a problem.

Fiona?  Are the watermarks unique to each recipient?

If they are, all I can think of is to make the code more efficient.  Perhaps run the water mark software in C if possible.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Expert Comment

ID: 36922581
@hmccurdy I didn't want to suggest that you weren't right, I was merely showing how I implement the idea you already proposed - except to allow for as many watermarks as needed. Our solution would reduce CPU/RAM usage significantly and increase HD usage. However HD storage is a dime a gig (maybe even a dime a tera!)

@fiona another problem could be the size of the images, if the resolution is very high you could think about resizing them down to a more reasonable resolution when you upload them. IE if the image will never be displayed higher than 1024x768 why store it at 3200x2400 anyway? Especially since it will hog more resources to watermark / output.

Author Comment

ID: 36923258
Derokorian: Your solution is along the lines of what I had already concluded I would have to do. I was hoping there might be some other solution that I hadn't thought of. I have the additional problem that photos are often changed (but file names aren't).
I think the simplest solution is to include all this in the image upload script, which already resizes all the images and creates the folders and thumbnails. I'll just get it to create an extra folder and watermark the images at the same time. That way I can be sure that the watermarked and unwatermaked images are the same.
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 1000 total points
ID: 36923692
@Derokorian -- No issues with you.  I didn't think you were suggesting I wasn't right.  I like your idea.

@Fiona -- I don't think there's a better approach than we've discussed.  I think you are right about an image upload script.  If you blend in some of Derokorian's code (or write your own) with the existing script, I think you'll have the best possible solution.

The only other idea I have is to push some of the PHP work off to C/C++.  Alternatively, compile the PHP code (something I've never done) so that it doesn't go through the interpreter. But I suspect the expense of development makes that solution "not best."

Author Closing Comment

ID: 36923755
Thank you both for your input. I have some code rewriting to do!
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36923872
You're welcome.
LVL 10

Expert Comment

ID: 36924326
Glad to help! If you need help rewriting code - feel free to post what you have and explain what you need and we'll be here!

Good luck!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question