PHP watermark images on the fly - php

Background
I have read through many sites including this one for a solution. I have a watermark script which up until a few months ago worked flawlessly. My hosting provider performed routine updates and hosed my script. I made one adjustment to it, it worked. With time, the watermark script seems be very heavy on system resources, causing all sorts of issues with my site including images not loading at random, etc. Currently, it appears I could run the site on PHP version 5.4 or a version or two above and below it. Currently running at 5.4 if that should help.
Explanation
The script is intended to find image files from a specific location, pending on size, combine with one of several png images on the fly without overwriting the original image.
The script
I have modified my script significantly in the past week, with minor performance improvement. I'm at my wits end, are there further improvements to this script to be made or cleaner code. Any assistance would greatly be appreciated.
Files
.htaccess, three jpg(s), and the 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 than 301 then apply watermark 600x600
elseif (imagesx($image) > 175 ) { $watermark = imagecreatefrompng('watermarksm.png'); } // height greater than 175 then apply small watermark 200x200
else { $watermark = imagecreatefrompng('empty.png'); } // apply a 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 I have tried not reflected in this script is the following "minor" change.
if(preg_match('/.gif/i',$src)) to if(preg_match('/\.gif$/i',$src))
Another variation attempted in the preg_match was with jpe$g and jp(|e)g$. Those change didn't seem to help in anyway and seemed to hurt the performance further.
Again, any guidance would be appreciated. Thank you in advance.

Why don't you once for all create watermarked version of all your images ? It will avoid the server to work each time an image will be displayed on your website, and you will gain in performance.
If for any reason you need to display the original image, do a script that check the credential of the viewer then return the image.

First of all, those regular expressions are not the performance hogs. The real performance problems comes from image manipulation.
Save the result from imagejpeg($image); into a file on disk "hidden" from the world. You can restrict access to that folder with .htaccess.
The the logic should be something like this:
<?php
// Implement getTempFile to get a path name to the hidden folder.
$tempsrc = getTempFile($src)
$tempsrcexists = file_exists($tempsrc);
if (!$tempsrcexists)
{
// Create image on disk.
...
}
// At this point, the temporary file must exist.
$fp = fopen($tempsrcexists, 'rb');
// Output data to the client from the temporary file
header("Content-type: image/jpeg");
fpassthrough($fp);
fclose($fp);
?>
This should reduce the load on the server significantly.

Related

PHP Multiple Image Scripts On One Image Source

I have 2 different scripts for handling images:
The first one is a watermark script for watermarking images on the fly:
$imgpath=$_REQUEST['filename'];
header('content-type: image/jpeg');
$watermarkfile="assets/img/logo_variations/logo_watermark_75.png";
$watermark = imagecreatefrompng($watermarkfile);
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
$image = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$dest_x = ($size[0] - $watermark_width)/2;
$dest_y = ($size[1] - $watermark_height)/2;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
So the image URL for a watermarked image is: http://example.com/watermark.php?filename=assets/img/temp/temp_share.jpg or since I'm using mod_rewrite to "pretty up" my URL: http://example.com/watermark/assets/img/temp/temp_share.jpg.
Works like a charm and my reason for doing so like this is because this is on a modeling website where I want to display the images without watermarks but I use a jquery script to change the image source of the image gets right clicked(assuming a user is trying to save the image).
The script only changes the source of any image with a class of img-protected.
I've written it to ignore any image with watermark in the URL so that it doesn't try to change the already watermarked image which would result in a url like: http://example.com/watermark/watermark/img.jpg which would result in a broken image. The other part is written to remove http://example.com from the original source so I don't end up with http://example.com/watermark/http://example.com/img.jpg.
$('.img-protected').on('mousedown', function (event) {
if (event.which == 3) {
if(this.src.indexOf("watermark") > -1) {
return false;
}
else {
src = this.src.replace('http://example.com/','');
this.src = 'http://example.com/watermark/' + src;
}
}
});
All of this works exceptionally well until I added another image handling script:
I'm using TimThumb.php which is an on the fly image resize script I use for creating gallery icons instead of uploading an icon and a full size image(this is how I wish to keep doing so as well).
The problem I am facing is this:
If I have an image that is being turned into a thumbnail using TimThumb.php which I renamed to thumb.php on my server the URL is http://example.com/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 which gives me an icon for 5361ae7de9404.jpg.
All of my icons have a class of img-protected which means on right click the above URL is going to be changed to the watermarked one.
This is where it fails.
The outputed URL when right clicked is http://example.com/watermark/http://www.example.com/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 which results in a broken image.
I manually tried making the URL into http://example.com/watermark/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 to see if that would change anything but it still results in a broken image.
What I need is to be able to also watermark the generated icons from thumb.php using watermark.php.
Is there a way to combine these two scripts or a workaround to make this work?
I'm at a complete loss here.
EDIT: I am fully aware that advanced users can still acquire the non watermarked image since it's already been downloaded to there device, but I don't expect a high volume of users to visit this particular website as this is simply a local models portfolio.

How to put a watermark text across the whole image in diagonal position?

I have connected my Google Drive with my website and im getting images in this format: https://googledrive.com/host/{$GoogleID}. What i want to do is to add a text (not image) watermark to all the images im getting from Google Drive. I already tried with:
http://php.net/manual/en/image.examples.merged-watermark.php
http://phpimageworkshop.com/tutorial/1/adding-watermark.html
Both of them dosent work for me or i cant get them to work i dont know why. I will give an example for an image url: https://googledrive.com/host/0B9eVkF94eohMRlBQVENRWE5mc2c
I have also tried the code from this answer, but it dosent work as well. I guess the problem should be that the files i'm getting from Google Drive are not with the file extention and maybe this cause the problem. This is only my guess...
UPDATE:
i managed to show the photo on the website, but how to put the text in diagonal possition across all the photo like this
try to read image with file_get_contents or fopen and create image from string.
$im = imagecreatefromstring(file_get_contents("image url"));
and then use this example: http://php.net/manual/en/image.examples.merged-watermark.php
Thanks to #Durim Jusaj for helping me out with this one and finally i got the answer after a lot of searching and dealing with a lot of problems i will share my knowledge with u guys. So i will post the code and i will explain what is the code doing.
First thing first. Include this function in your file. This function is finding the center of the image. I havent wrote it i found it in the php docs under the comments so maybe it can be done without it or this function can be modified to be better written.
function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
{
// retrieve boundingbox
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
// calculate deviation
$dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0; // deviation left-right
$dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0; // deviation top-bottom
// new pivotpoint
$px = $x-$dx;
$py = $y-$dy;
return imagettftext($im, $size, $angle, $px, $py, $color, $fontfile, $text);
}
Load the image you want to apply the watermark to. I'm using Google Drive to extract my photos so thats why im using the file_get_contents, if you are the case like mine then use this, BUT otherwise use what is common and if your picture have extension like .jpg or .png you can use imagecreatefromjpeg or imagecreatefrompng. So it should be something like that $im = imagecreatefromjpeg('photo.jpeg'); for example.
$im = imagecreatefromstring(file_get_contents("https://drive.google.com/uc?id=" . $v['GoogleID']));
After that we need to calculate the position of watermark so we want the watermark to be in the center and to auto adjust its size based on the size of the photo. So on the first like we store all the attributes of the image to array. Second and third lines we calculate where is the center of the image (for me dividing it by 2.2 worked great, you can change this if u want to adjust the position. Last line is the size of the watermark according to the size of the image. This is very important as the watermark will be small or very big if u have images with different sizes.
list($width, $height, $type, $attr) = getimagesize("https://drive.google.com/uc?id=" . $v['GoogleID']);
$width = $width / 2.2;
$height = $height / 2.2;
$size = ($width + $height) / 4;
Set the content-type
header('Content-Type: image/png');
Create the image. I dont know why it should be done twice, but im following the php manual.
$im = imagecreatefromstring(file_get_contents("https://drive.google.com/uc?id=" . $v['GoogleID']));
Create some colors. So, if you want your watermark to be transparent (like mine) you have to use imagecolorallocatealpha, otherwise use imagecolorallocate. Last parameter is the transparency. You can google every function name for more info from the php doc.
$black = imagecolorallocatealpha($im, 0, 0, 0, 100);
The text to draw. Simple as that. Write what you want your text to be.
$text = 'nima.bg';
Replace path by your own font path. Put a font in your server so the code can take it (if you dont have already).
$font = 'fonts/ParsekCyrillic.ttf';
Adding all together. Basically you are creating the final image with the water mark with this function. The magic ;)
imagettftext_cr($im, $size, 20, $width, $height, $black, $font, $text);
Now this is the tricky part! If you want to just display the image without saving it to your server you can simply copy and paste this code, but the website will load very slow if you have more then 2-3 images. So, what i suggest you to do it to save the final images to your server and then display it. I know you will have duplicates, but this is the best choice i think.
ob_start();
imagepng($im);
$image = ob_get_contents();
ob_end_clean();
imagedestroy($im);
echo "<img src='data:image/png;base64," . base64_encode($image) . "'>";
}
OR you can save the images to your server and then display them which is much much faster. The best thing you can do is to process all the images create them with the watermark and then display them (so you have them ready to be show when a visitor visit your website).
imagepng($im, 'photo_stamp.png');
imagedestroy($im);
And the final result for me was that.
UPDATE: As of 2017 you can extract the image using this link 'https://drive.google.com/uc?id=(GoogleID)' or you can just use the 'webContentLink' property of the Google_DriveFile Object (it will give you the same link).

Gallery loading slowly [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Thumbnails in php generated gallery loading slow
Code link can be found on the bottom of the page.
The site is running from 1and1.co.uk linux based system, good enough to load the gallery much faster than it currently does.
The php generated thumbnails are loading a bit slow, can you tell me why?
http://site-perf.com/cgi-bin/show.cgi?id=vz5le19Fp5E
code:
http://satinaweb.com/tmp/functions.txt
Here is crop.php:
$sourceimage = $_GET['a'];
function crop($sourceimage) {
// Draw & resize
header('Content-Type: $imsz[\'mime\']');
list($width, $height) = getimagesize($sourceimage);
if($width > $height){
$new_width = 100;
$new_height = 75;
} else {
$new_width = 75;
$new_height = 100;
}
$image = imagecreatefromjpeg($sourceimage);
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p);
imagedestroy($image_p);
imagedestroy($image);
}
crop($sourceimage);
If you have questions, please ask!
What you should have noticed from your site-perf graph most is that,
I'm not going to look through all your code, but you have a file called crop.php. This is going to be slow because it (presumably) crops the images on every page load, which takes a relatively long time (made apparent from the site-perf data); for each request, the browser spends most of it's time waiting for the server to respond.
You should consider using a cache folder to store cropped images and serve them up as-is to reduce loading time.
In crop.php, you could do something like this (pseudo code):
IF original image newer than cropped OR cropped doesn't exist
Load original image
Crop original image
Save cropped image to cache folder
ELSE
Serve already-cropped image from cache
ENDIF
Every time you call crop(), you read the image, modify it and spit it out to the client. This is incredibly wasteful, because you need to re-process the image for every request. You're even destroying the image afterwards (not that this makes much difference). Instead, use is_file() with a cache directory, and save the image to disk as well as sending it to the client.
Now, your script might look like this:
$sourceimage = $_GET['a'];
$cache_dir = dirname(__FILE__) . "/cache"; // Cache directory
$cache_file = $cache_dir . '/' . $source_image; // Path to cached image
function crop($sourceimage) {
header('Content-Type: $imsz[\'mime\']');
// If cached version of cropped image doesn't exist, create it
if(!is_file($cache_file)) {
list($width, $height) = getimagesize($sourceimage);
if($width > $height) {
$new_width = 100;
$new_height = 75;
} else {
$new_width = 75;
$new_height = 100;
}
$image = imagecreatefromjpeg($sourceimage);
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $cache_file); // Save image file to disk
} else { // If cached file exists, output to browser without processing
echo file_get_contents($cache_file);
}
}
crop($sourceimage);
This is untested, but should work. Please don't just copy and paste the above code; read through it and make sure you understand how and why it does what it does.
Your page is loading slowly due to HTTP requests being queued.
Although each image is small, the requests are spending a large proportion of their time queued, waiting for other requests to finish before they start, in the worst cases, they are waiting for one and a half seconds before they even start downloading.
The second delay is then caused by the time it takes to create the images.
The actual download was nearly instant after waiting for these other processes.
Queued 1600ms
Waiting 986ms
Download 0ms
Rather than generate the correct sized images every time, why not store the result to be used on subsequent visits - this means the page will be slow once and then much faster all other times.
If the time your script took to crop the images was shorted, the queueing would be shorter too. Here is an example...
Check in a folder for the cropped image
If it exists, serve it (no computation required)
If it doesn't exist, generate it
Save the result in the folder to cache it

Create thumbnails using PHP or manully add thumbnails?

I have a Folder called "Gallery" inside which i have created a folder for Thumbails called "Gallery-Thumbs", where i manually add thumbnails.
My question is whether it is better to manually add thumbnails or create thumbnails dynamically using "GD" or "ImageMagick".
Problems with adding Thumbnails manually
If i forget to add a thumbnail for photo the chain of the Gallery breaks
It is tedious to resize all the images for creating the thumbnails
So does using PHP ImageProcessing function add additional overhead in creating thumbnails or is the correct approach?
-- Updated for answering Queries
How you add images to the "gallery" folder?
Through Direct file upload
How those images and thumbnails are accessed?
I use glob to get list of files in respective folders
How do you (want to) map images to thumbnails?
For thumbnails i use imageName-thumb.jpg,
so they appear in the list in the same order as they are in main image folder
Use PHP to create thumbnails once and save them in a "thumbnails" directory with the same file names. Then use these now-ready-to-be-used thumbnails directly.
If your going with some Dynamic image processing tool then it will be little bit time consuming process, except that it will be a great technique.
If you are going with the manual process then:
Make thumbnail compulsory using some client side script
It depends on your image orientation
I create the thumbnails during upload and either save in a different directory with the same name or add th_ to the front of the filename.
You could also upload some code to generate all the thumbs again for the images you currently have in your gallery in case you have missed any.
You could also resize to different sizes at the same time, watermark or add other effects.
This is my gallery and I have a thumb and normal size; the thumb was sharpened on upload and the normal watermarked. Bother versions have a drop shadow and the resizing etc. was done with Imagemagick.
http://www.rubble.info/gallery/
If you check the file is there before displaying it and it is missing you can show a default image.
You can check out my website for lots of different things you can do with php and Imagemagick.
I highly recommend using GD with some sort of caching to keep the ones that don't change. There is, however, already a superb library for doing this. It's an absolute favourite of mine and gives you easy image compression and resizing just with a GET URL.
Try Smart Image Resizer by ShiftingPixel:
http://shiftingpixel.com/2008/03/03/smart-image-resizer/
You can, if you wish, use a website I made as a reference using its page source: http://www.eastwood-whelpton.co.uk/about/gallery.php
Nearly all images on this website use the Smart Image Resizer GD library.
I can also provide the PHP code I used if you wish for examples. This particular code automatically adds any images found in my gallery folder to this page.
As stated previously you need PHP 5 with GD support. If you have these then here is a very handy function to create thumbnails of a given dimensions and quality ($options) from every image in a given directory ($from_dir) and save them to another directory ($to_dir).
function make_thumbnails($from_dir,$to_dir, $options){
$files = scandir($from_dir);
$exclude = array('.','..','etc.');
foreach($files as $fi => $fv){
if(!in_array($fv,$exclude)){
$from_file = $from_dir.$fv;
$to_file = $target_dir.$fv;
list($img_width, $img_height,$img_type) = getimagesize($from_file);
$scale = min($options['max_width'] / $img_width,
$options['max_height'] / $img_height
);
$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
$new_img = imagecreatetruecolor($new_width, $new_height);
$src_img = imagecreatefromjpeg($from_file);
$success = $src_img && imagecopyresampled(
$new_img,
$src_img,
0, 0, 0, 0,
$new_width,
$new_height,
$img_width,
$img_height
) && imagejpeg($new_img, $to_file, $options['quality']);
//Monitor results with $success - returns 1 or null
echo '<br />success:['.$success.']';
}
}
}
//Set options
$from_dir = ':/source/dir';
$to_dir = ':/destination/dir';
$options = array();
$options['max_width'] = 100;
$options['max_height'] = 100;
$options['quality'] = 100;
// Make thumbs...
make_thumbnails($from_dir,$to_dir,$options);

Image uploading concept in PHP

how to take image from one folder and rename and re-size the images and move to another folder?
I need to get image from one folder and rename and re-size the same image and move into another folder using php
You will most likely be using gd for resizing the images.
Here is a pretty crappy, but hopefully useful code sample. In this case, $originalName is the name given in the $_FILES array's tmp_name position. I am resizing to a width of 1200 in this case, with the height adapting according to such width. You might (and most likely will) not desire this behavior. This is just some ugly code I used in some courses I taught about 3 years ago, I don't have the newer samples in this computer so you will have to get used to the idea :)
$newDir is where the file will be located. by calling imagejpeg or imagepng and passing the filename as second argument, it means to the function that you wish to save the image in that location.
if ($type == 'image/jpeg') {
$original = imagecreatefromjpeg($originalName);
}
else {
$original = imagecreatefrompng($originalName);
}
$width = imagesx($original);
$height = imagesy($original);
//prepare for creation of image with width of 1000
$new_height = floor($height * (1200 / $width));
// create the 1200 width image
$tmp_img = imagecreatetruecolor(1200, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $original, 0, 0, 0, 0,
1200, $new_height, $width, $height);
//create a random and unique name to identify (here it isn't that random ;)
$newDir = '/this/is/some/directory/and/filename.';
if ($type == 'image/jpeg') {
imagejpeg($tmp_img, $newDir."jpg");
}
else {
imagepng($tmp_img, $newDir."png");
}
Many file system functions are already built-in with PHP (e.g. rename), and you'll find most of what you need to resize images by using the GD library here.
There are libraries available in PHP for image resize.
Here are some useful links you may like.
http://www.fliquidstudios.com/2009/05/07/resizing-images-in-php-with-gd-and-imagick/
http://php.net/manual/en/book.image.php
PHP/GD - Cropping and Resizing Images
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
Use imagick http://php.net/manual/en/imagick.resizeimage.php
If I were you I would write a PE using a diffent language (one that you might be best used to) to adjust anything of the given image then just feel free to phpexec it to do all the steps you mentioned, you can sit relax and wait for the end result. HHAHAHHA :-)
Use imagick library to resize; it's good.

Categories