PHP watermark images on the fly -
background
i have read through many sites including 1 solution. have watermark script until few months ago worked flawlessly. hosting provider performed routine updates , hosed script. made 1 adjustment it, worked. time, watermark script seems heavy on system resources, causing sorts of issues site including images not loading @ random, etc. currently, appears run site on php version 5.4 or version or 2 above , below it. running @ 5.4 if should help.
explanation
the script intended find image files specific location, pending on size, combine 1 of several png images on fly without overwriting original image.
the script
i have modified script in past week, minor performance improvement. i'm @ wits end, there further improvements script made or cleaner code. assistance appreciated.
files .htaccess, 3 jpg(s), , watermark.php script.
.htaccess
rewriterule ^(.*)wp-content/uploads/(.*) $1watermark.php?src=wp-content/uploads/$2
watermark.php
<?php $src = $_get['src']; if(preg_match('/.gif/i',$src)) { $image = imagecreatefromgif($src); } if(preg_match('/.png/i',$src)) { $image = imagecreatefrompng($src); } if(preg_match('/.jpeg/i',$src)||preg_match('/.jpg/i',$src)) { $image = imagecreatefromjpeg($src); } if (!$image) exit(); // if (empty($images)) die(); if (imagesx($image) > 301 ) { $watermark = imagecreatefrompng('watermark.png'); } // height greater 301 apply watermark 600x600 elseif (imagesx($image) > 175 ) { $watermark = imagecreatefrompng('watermarksm.png'); } // height greater 175 apply small watermark 200x200 else { $watermark = imagecreatefrompng('empty.png'); } // apply dummy watermark resulting in none. 1x1 $dest_x = imagesx($image) - imagesx($watermark) - 0; $dest_y = imagesy($image) - imagesy($watermark) - 0; imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, imagesx($watermark), imagesy($watermark)); header('content-type: image/jpeg'); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); //die(); ?>
a couple of things have tried not reflected in script following "minor" change.
if(preg_match('/.gif/i',$src))
if(preg_match('/\.gif$/i',$src))
another variation attempted in preg_match jpe$g
, jp(|e)g$
. change didn't seem in anyway , seemed hurt performance further.
again, guidance appreciated. thank in advance.
why don't once create watermarked version of images ? avoid server work each time image displayed on website, , gain in performance.
if reason need display original image, script check credential of viewer return image.
Comments
Post a Comment