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.
Related
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.
Iam working on a project , which involves fetching images from various websites and displaying a Thumnail or resized version of orignal image.
As i will be fetching many images at time , this takes more time , As Orignal image files will be loaded.
What i need is given an image url , i need resized version of that image file? So that i need not download the Large orignal image file...
for example : orignal image : 500X800 some 500kb
what i need is : 100X150 some 100kb image file..
Is it possible using Jquery? OR PHP?
Are their any functions like imagecopyresampled(PHP) in Jquery or any plugins?
Well, the big file needs to be downloaded at one point to create the thumbnail.
What you can do, through JQuery, is ask your server for the thumbnail, the server downloads the file, creates the thumbnail and sends back to the client the URL of the thumbnail when the resizing is done. This way, the client will gradually receive thumbnails as they are ready.
Edit After experimenting a bit, I found out that as soon as an img requests a picture, even if you dynamically remove its src attribute, the download continue.
So I decided to write a sample code (there's not much validation and it only works for jpeg, adding the other types would be really easy though). The solution is divided in 3 parts:
HTML
First, I used an empty div with some specific attributes to let jQuery what to load. I need to apologize to Nuria Oliver, she had the first "big picture" I could find on the net. So, here's a sample div:
<div class="thumbnail" data-source="http://www.nuriaoliver.com/pics/nuria1.jpg" data-thumbnail-width="100" data-thumbnail-height= "200"/></div>
I use thumbnail as class to be able to find easily the divs I want. The other data parameters allow me to configure the thumbnail with the source, width and height.
JavaScript / jQuery
Now, we need to locate all these divs requesting thumbnails... using jQuery it is pretty easy. We extract all the data settings and push them in an array. We then feed a PHP page with the query and wait for the response. While doing so, I'm filling the div with some HTML showing the "progress"
<script type="text/javascript">
$(".thumbnail").each(function() {
var img = $(this);
var thumbnailWidth = img.data("thumbnail-width");
var thumbnailHeight = img.data("thumbnail-height");
var thumbnailSource = img.data("source");
var innerDiv = "<div style='width:" + thumbnailWidth + "px; height:" + thumbnailHeight + "px'>Loading Thumbnail<br><img src='loading.gif'></div>";
img.html(innerDiv); // replace with placeholder
var thumbnailParams = {};
thumbnailParams['src'] = thumbnailSource;
thumbnailParams['width'] = thumbnailWidth;
thumbnailParams['height'] = thumbnailHeight;
$.ajax({
url: "imageloader.php",
data: thumbnailParams,
success: function(data) {
img.html("<img src='" + data + "'>");
},
});
})
</script>
PHP
On the PHP side of things, I do a quick test to see if the picture has already been cached (I'm only using the file name, this should be more complicated but it was just to give you an example) otherwise I download the picture, resize it, store it in a thumbnail folder and return the path to jQuery:
<?php
$url = $_GET["src"];
$width = $_GET["width"];
$height = $_GET["height"];
$filename = "thumbnail/" . basename($url);
if(is_file($filename)) // File is already cached
{
echo $filename;
exit;
}
// for now only assume JPG, but checking the extention should be easy to support other types
file_put_contents($filename, file_get_contents($url)); // download large picture
list($originalWidth, $originalHeight) = getimagesize($filename);
$original = imagecreatefromjpeg($filename); // load original file
$thumbnail = imagecreatetruecolor($width, $height); // create thumbnail
imagecopyresized($thumbnail, $original, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight); // copy the thumbnail
imagedestroy($original);
imagejpeg($thumbnail, $filename); // overwrite existing file
imagedestroy($thumbnail);
echo $filename;
Browser
So, what does it do exactly? In the Browser, when I open the page I first see:
And as soon as the server is done processing the image, it gets updated automatically to:
Which is a thumbnail version, 100x200 of our original picture.
I hope this covers everything you needed!
PHP Thumb is a good plugin it worked for me and easy to operate.
And the best part is there are many options to select about the output file like its quality, max width for potrait images , max width for landscape images etc..
And we can set permissions like block off server requests, block off domain thumbnails.. etc
Note: this was actually posted by someone , as an answer to this question..But it was deleted.So iam reposting it, as it was useful.
for this to work you need to do some actual image processing which you cant do with js, so some server side language is needed.PHP is an option
If you are not interesting in obfuscating the image source, you may very well rely on an external tool like http://images.weserv.nl/ which takes an original image and allows you to request it in a different size.
If you end up not relying on a global CDN, at least sort out proper caching. Last thing you want to do is resize the same image over and over again for subsequent identical requests - any benefit of image's lower size will probably be negated by processing time.
I am testing a script where I watermark an image in my webpage.
The script works fine and the image is watermark but my problem is that only the image is displayed on the page.
As soon as I add the script to my page it's like the web page is converted to the image that I'm watermarking.
I think it's because of header("content-type: image/jpeg"); from the code.
I need to watermark the image on my webpage but I also need the rest of my webpage to be displayed too.
How is this done? I'm quite confused on how this works.
The script I'm using is from here
Here's the code I'm using:
<?php
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 100; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
NOTE: I'm getting the image path from the database so I cannot hardcode the image filename as it's dynamic.
This is a bit mind-boggling if you're doing it for the first time, but once you see how it works it's really simple. ;)
You need one script that generates the image (e.g. image.php) and then your main script references that image (e.g. <img src="image.php">). It's not possible to have one request/script return both a document and an image simultaneously.
(PS. it's actually possible but it involves encoding the image as a very weird kind of src attribute. You don't want to do it, trust me.)
I believe you got confused about how this works: The watermark generating code above is not meant to be included in the page generating scripts/logic. Instead, it needs to be accessible behind it's own URL and then linked via <img src="./path/to/script"/> into the site you want to contain that image.
Put this script entirely separated from your "main website" script. On your website, use an <img> tag, with that watermarking script as the source.
You can't combine two different content-types like you're appearing to do. If you want a "web page" to appear, the content-type needs to be text/html. If you want just an image to appear, the content-type needs to be image/*.
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);
I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.