PHP image on the fly resizing with caching, and croping.
Edit: Feb 5th 2012 1:34 PM
Thanks to palepurple some needed updates have happened to this script.
Changes:
-
2012/01/30 - David Goodwin - call escapeshellarg on parameters going into the shell
-
@param string $imagePath - either a local absolute/relative path, or a remote URL (e.g. http://...flickr.com/.../ ). See SECURITY note above.
-
@param array $opts (w(pixels), h(pixels), crop(boolean), scale(boolean), thumbnail(boolean), maxOnly(boolean), canvas-color(#abcabc), output-filename(string), cache_http_minutes(int))
-
@return new URL for resized image.
Edit: July 1st 2009 1:46 AM
There is a new version of php image resize which now supports remote images, you can download it now or visit the github page here:
Visit PHP Image Resize (GitHub)
There is also a new example page included in the repository.
---------------------------------------------------------------------
Just thought I would share a recent function that we have built to resize images on the fly, so if you have an image that is 800x800, and you want to make a thumbnail to 200x200 just use the function like this:
<img src="<?php echo resize("PATH_TO_IMAGE",array("w"=>200,"h"=>200)); ?>" border="0" />
Be sure to check out the example page.
Configuring is pretty simple, just make sure the cache directory that you choose is writable by the server, and you should be up and running.
The script will compare the generated images timestamp to the original image's timestamp, so if the original image is changed all cached images will be re-built.
I hope you enjoy, and let us know if you have any questions, thanks!






























171 COMMENTS
Question. If an image has a transparent background like a .png, it receives a white one when it's resized. Is there any way to change the background color so that it matches the site better?
regards,
My original code is:
printf ("<a href='files/%s' target='_blank'><img src='files/%s' width='200' border='0'></a>", $myrow['pilt'], $myrow['pilt']);
I dont understand why can i made this code work width image resizer - i haw only error
First of all I get the following error messages:
Notice: Undefined index: h in C:\Users\paulatop\wamp\www\temp\resize.php on line 22
Notice: Undefined variable: h in C:\Users\paulatop\wamp\www\temp\resize.php on line 32
Notice: Undefined variable: h in C:\Users\paulatop\wamp\www\temp\resize.php on line 58
Notice: Undefined index: maxOnly in C:\Users\paulatop\wamp\www\temp\resize.php on line 83
I am able to remove the notices by using the suggestion from here: http://www.dmxzone.com/go?13811
I have configured the cache folder correctly, I echoed the $cacheFolder to be sure.
The thumbnails simply don't generate.
I've been testing this with wamp on vista and xp. Both have the same outcome.
Do I need a plugin such as ImageMagick to make this work?
Yes ImageMagick is required for this script. In some cases the 'convert' command path to ImageMagick may vary on different servers.
I installed ImageMagick and got the demo working. I've been playing around with it a bit and found a couple of things:
- Images with spaces in their names don't work. Maybe a str_replace can be used somewhere to fix this.
- on line 25 there is the following line of code:
$imgPath = str_replace('.'.$ext,'',$imagePath);
I can't find the variable $imgPath anywhere else, so not sure if it is needed?
- to find the maximum allowable height or width change the line:
if($width > $height){
to:
$widthPercentage = $width / $w;
$heightPercentage = $height / $h;
if($widthPercentage > $heightPercentage){
Hope these are constructive.
exec("convert ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath);
To:
exec("convert \"$imagePath\" -resize ".$resize." -quality ".$quality." ".$newPath);
It run perfectly in my localhost testing but it didnt work online.
exec("convert ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath);
Is it because this line of command only work in window but not linux??
The script is designed to use ImageMagick and PHP, if you have those two installed and working properly, check your cache folder's permissions. Let me know if your getting any kind of errors.
Speng,
You may want to check your online machine's path to convert. Try "whereis convert" and see if you can find convert in a different location.
Yup, I've actually got a new version of the script that supports external image requests then cache's the resized image locally. I'll be posting a version on github soon, i'll link to it here when its ready! Thanks for the interest in the script.
please tell me what is the mistake.
It could be a number of reasons, what kind of server environment are you running? Do you have ImageMagick running? Have you checked your cache folder's permissions? Make sure your site can write to your cache folder and be sure you have ImageMagick configured correctly.
Thanks
http://filmnagaram.com/resize/example.php
:( what may be the problem??
Finally an image resize script that works.
Now I only have one issue: can we have ZoomCrop?
I'd like to get rid of the black bars on the side when cropping, I want the image to fill out the entire dimensions I specify.
Like timthumb ;)
Oh, and another issue: I'm trying to get this done in WordPress. Since WP uses absolute links, this script interprets these as external links ... any idea how to avoid this duplication of images that already are on the server?
Look for
<b># check for remote image..</b>
Under this line you'll find a bit of code that checks the image url if it contains "http".
Ich changed this to
<b> if((ereg('http://',$imagePath) == true) && (ereg('http://www.mores.cc',$imagePath) == false) ):
<b>
For my domain, obviously.
After this check, add this to get rid of the absolute url and make it a relative url:
<b> if (ereg('http://www.mores.cc',$imagePath) == true):
$imagePath = ereg_replace('http://www.mores.cc', '', $imagePath);
endif;</b>
Now, how do I get zoomcrop :)
mores: Looks like you got this figured out, you could have just linked to the http://yoursite.com/image.jpg in the resizer var.
When it looks for an image it copies it over and caches it so it doesn't necessarily need to grab the external image everytime :) Save some bandwidth here and there
I haven't implemented it into wordpress before, but I imagine it is pretty similar to a standard php installation.
Find your global include file and include the function into the wordpress site. Then use the resize function to wrap your image src just like the documentation states, also make sure you have ImageMagick and your cache folder configured properly.
If you're extracting post images etc it'll be a bit complicated, too much so to post in this little box :)
Right now I'm using it in a wordpress installation that's running in a subdirectory and it fails - must be some path issue.
Hello, I installed your script and I think I'm doing something wrong, because I do not work either locally or remotely, have given permission (777) to the images directory. If you please take a look at http:www.mineralssalvat.com/example.php, I've also found that the image is seen in www.mineralssalvat.com / images / dog.jpg and this is correctamente.Tambien I enclose configuration www.mineralssalvat.com php / info.php.
Sorry but my English is very low
Greetings
had also trouble getting remote image too work. finally i discovered that jou just have to add s directory remote in the cache dir..
plz. explain what is imagemagic, what it does. also explain more about the folder/directories - cache?? convert??
i have a temp directory on my c: drive and the 'uploads' folder for storage.
and plz put full step by step inst. for us novice who can then make use of your work.
i need to resize my file ie. reduce it's file size when uploading and saving to my server? can this script do it?
help me and a coffee is coming up your way.
thanks.
then i had to poo
Thanks!
Andie
I'm sorry I haven't gotten back to some of the issues posted here. Imagemagick is a software suite that runs on the command line. It interfaces with php many other programming languages. More can be learned about imagemagick @ http://imagemagick.com.
Yes, to get remote images working, you will need to add a folder for remote images in your cache folder, there is a configuration for this in the function if you wish to place it elsewhere.
This script isn't meant to work when uploading the file, but rather when calling it inline on your code. It takes the originally sized photo and resizes it down. So you'd place the code in a 'View' or plain html / php file.
To save a new version of the photo, it should auto detect if the newer photo is "newer" than the cached one.. You can also clear out the cache folder to regenerate all you images again.
Thanks!
@arif - this is not a beginners script so if you have a project in mind and you'd like to use it, begin by reading about what ImageMagick is, and don't complain to the script provider for not having guided hand in hand.
First of all, this script is very good and it is working perfectly on my server.
But now i'm facing some troubles on it.
On my testing I made a new folder in the root directory and put all required files,folders & images inside it. Then I tested the URL like (www.mysite.com/demofolder), and it was perfect.
Then I tried to implement it on various images on the website itself. This time its showing 'Not found' images. My images are residing in different folders and subfolders but website have a master page, so each and every requests are passing tru index.php, so i made 'cache'(777) folder in the root directory where the index.php residing.
Can anybody help me where is the problem ? any directory structure issues ??
It sounds like your having some kind of path problem to your images. The best method is to use absolute paths to your webroot path.
/path/to/images/image.jpg
Something like that should work, just as long as your image is located at that absolute path.
Thanks
No luck with absolute path too.
Also I wondered by another behaviour.
As I told in previous post, its working perfectly in demo folder (ie; www.mysite.com/demofolder).
And then I put all files,folders & images in the root directory itself (ie; www.mysite.com/example.php).
Its showing only one image ( $settings = array('w'=>300,'h'=>300,'scale'=>true);)
and all others not appeared.
Do you know why?
For some strange reason, an external file can be written into the "external" directory, and convert seems to work since the script returns a file name that I can use, but that file does not exist.
I'm not sure where I can start looking for bugs. The directories are, apparently, writeable, and convert must be found or else I would not get a proper file name. Right?
I'm hosted at emerion and the directories are not CHMODable by me, but the tech guys tell me that php scripts can write anyways.
Any ideas?
I'd need to look at the code to see what exactly is happening. It still sounds like a path issue, maybe permissions? Can you post some code up on gist.github.com?
Hi Daniel,
Make sure those external directories that you are writing to are writeable by your webserver. In most cases that would be apache. If it is generating the md5 serial hash filename then things should be good, just sounds like a permissions issue. You might want to check your error logs for the site to find some kind of clues.
Thanks
thank you for getting back to me so quickly.
I don't have access to a lot of things on that host, I can't even use my FTP program's CHMOD function.
The host tech guys said "don't worry about it" and sure enough, a file has been generated in the cache/external directory. Which means that the directories *are* writeable by scripts.
But the files generated by convert are not, apparently.
Can this be?
You might want to make sure you have the correct path to convert. Depending on hosts, sometimes it will just be available as 'convert'.. Or you might need a full path to the function. '/usr/bin/convert'
There are definetely a few things that can go wrong due to configurations of server enviroments. No server is the same :)
I can take a quick look at your configuration / setup if you wanna send me some code.. Email it over to wes@joedesigns.com
Thanks
I don't think that an issue with path because one of the method is working fine (the scaling one) and the 'canvas-color' method not working.
I tried following code, there are 2 methods (scaling & canvas-color)
First I put this file in root directory itself (www/) there i can see
-First image(canavas-color method) - not showing
-Second image (scaling method) - showing fine
Then put it in another folder in root directory (www/testfolder/) there both images are fine.
So it seems that 'canvas-color' method have some issue when it is in root directory, actually that is what i looking for.
any clues?
==================================================================
<?php
include 'function.resize.php';
$settings1 = array('w'=>115,'h'=>60,'canvas-color'=>'#FFFFFF'); ?>
<img src='<?php echo resize('logos/8.jpg',$settings1)?>' border='1' />
<?php $settings2 = array('w'=>115,'h'=>60,'scale'=>true); ?>
<img src='<?php echo resize('logos/8.jpg',$settings2)?>' border='1' />
=================================================================
thanks for the script!.
i would like to request a copy of the external image source - resize - cache - use local image copy. i noticed on one comment that is now ready.
thanks in advance.
i would be following this post. :) subscribe to comments plugin is also a convenient feature. :)
I had to modify a section of code because if you crop an image with the width and height not being equal, you'd sometimes get the canvas instead of a proper crop.
I had to add this to the cropping section:
if (($h/$height)*$width>=$w) $resize = ($h/$height)*$width;
else if (($w/$width)*$height>=$h) $resize = "x".($w/$width)*$height;
Wes please confirm this is accurate...
$cacheFolder is from the document root of your server.
So is the url of the image itself, only the script checks this for you, not for $cacheFolder though, you need to specify this literally, so if your image is here: mystime.com/img/image.jpg, then this is your literal path: $_SERVER['DOCUMENT_ROOT']."/img/image.jpg".
$path_to_convert , as reported above is very different on many servers. If you dont have direct access to your account you can do the following in php, assuming 2 things: Image magick is in fact installed somewhere, and you do have eval() rights turned on...
try this in php:
eval('whereis convert', $output); echo var_dump($output))
This was the only way for me to confirm I had the right path to the imagemagick libs.
I am using xampp.
how did I know my cache folder and path to convert?
Thanks
I am trying to get this to work with the image url populated from database info, i.e
<img src="<?=resize($linkdir . $uploadfile_bground, $resize_settings)?>" alt="" />
This doesn't seem to work, however works fine when hardcoding the image url into it as in below...
<img src="<?=resize('/uploads/image1.jpg', $resize_settings)?>" alt="" />
Im sure theres something I'm missing, but can't find what
u might want to concat the variables prior to sticking them in the argument.. ex:
$filepath = $linkdir.$uploadfile_bground;
also debug that variable to see if its the correct path to your files. otherwise it should work
Thanks.
I have tried that, and although something is now happening, I don't know that this has been the cause.
What I am now seeing is that certain images are being resized and will show on the page, whilst others arent.
I am getting a cacheed image location load into the src for the image, but there isnt anything actually within the cache folder for that image.
Its odd as it does work with some images.
I have tried various image types, but that doesnt appear to be the cause, as it is working for each image type, just randomly.
Any ideas?
THanks again
Are all the images the same size relatively? same dpi at least? Its possible ImageMagick might be choking if the images are too large.
You should see temp images building into your cache folder. If you don't see any images added into your cache folder then you might be experiencing another problem. I'd need to see more code examples to help much further..
Got it sorted, well no idea how I did, it just seemed to work. Perhaps it was as you say something to do with the size of the images, as some of them were 6m!!
I have just limited the size the gallery will take during the upload, which will prevent this.
I have another issue now however, I am trying to deploy this onto a wamp machine (2003). I've got imagemagick installed, but seem to be having issues determining the path to convert...
I have: C:\Program Files\ImageMagick-6.6.2-Q16\convert.exe
however am getting the error below when trying to load my page.... (Incidentally this worked 100% on my linux test box).
PHP Warning: exec(): Unable to fork [C:\Program Files\ImageMagick-6.6.2-Q16\convert.exe 'uploads/7-DSC02203.JPG' -resize 80 -size 80x50 xc:transparent swap -gravity center -composite -quality 100 cache/d41d8cd98f00b204e9800998ecf8427e_w80_h50_cp.JPG] in C:\Inetpub\Websites\intertech\www\Scripts\function.resize.php on line 107
Solved this one, I had the path to imagemagick right, however if on windows you need to give IIS permission to execute scripts using the cmd shell.
To do this open up a cmd window (Start>Run>cmd) and enter the following command (You can copy and paste.....
cacls %COMSPEC% /E /G %COMPUTERNAME%\IUSR_%COMPUTERNAME%:R
Hope that helps someone :)
It does not work. Maybe it is because there is no .jpg? o .gif extension?
Yes, thats probably the problem your having, you should have the extension in your filename.
I am using your script, but when i resize, sometimes images are not resized proper. I can only see a part of the image..
url: http://www.pc-teros.es/carrusel/example.php
y en caso real me sale este error: "Cannot redeclare resize() (previously declared in /homepages/4/d284870012/htdocs/pc-teros/carrusel/function.resize.php:9) in /homepages/4/d284870012/htdocs/pc-teros/carrusel/function.resize.php on line 120"
url: http://www.pc-teros.es/carrusel/carru.php
¿me pueden ayudar?
Can't wait to try it.
Some images are correct but then some are not resized correctly
everything is solved. and is working perfectly here:
http://www.pc-teros.es/carrusel.php
example: http://www.pc-teros.es/carrusel/cache/a19e345bb85dc61a10e1bb2dc0fd1400_h128.jpg
thank you very much for the script.
if(isset($opts['scale']) && $opts['scale'] == true){
exec($path_to_convert." ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath);
}else{
exec($path_to_convert." ".$imagePath." -resize ".$resize." -size ".$w."x".$h." xc:".(isset($opts['canvas-color'])?$opts['canvas-color']:"transparent")." swap -gravity center -composite -quality ".$quality." ".$newPath);
}
}elseif(!empty($w)){
exec($path_to_convert." ".$imagePath." -thumbnail ".$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath);
}elseif(!empty($h)){
exec($path_to_convert." ".$imagePath." -thumbnail x".$h."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath);
}
or system might work depending on your server.
rather than
exec($path_to_...
use
system($path_to_...
or
passthru($path_to_...
true: http://www.total-soft.net/Carrusel/carru.php
Example: http://www.total-soft.net/Carrusel/example.php
know any script to do the same as this, but only using the GD library functions.
thanks for your help and your time.
If I resize the picture to 240*160 it cuts out the center part of the picture. What if I want to resize but keep total picture?
You should be able to use crop=>false,scale=>true..
I believe that will solve your problems.
Just a question: is possible add a watermark to images after they are resized?
Thank you
Hi wes using scale does not fix it because then it only resizes from one side. What I want is a complete picture resized to the proportion needed and add whitespace if needed, hope this makes sense to you?
You can add watermarking into this script fairly easily.. Check this out:
http://gist.github.com/540817
You might need to tweak folder paths and such..
Hey Thomas,
If you take a look at the demo page, it has the same example your looking for.
I believe you'll want to only define width and height for your resize.. This will resize and add canvas space around the image.. you can also define the canvas-color.. If your using png's I think the canvas should be transparent by default
To return the favor...
@Thomas (and @wes, if you accept the suggestion or if you have improvements to do ...I'm just a poor web designer...).
Like Thomas I wanted to resize images but keep total picture, so I changed this part of your script:
I hope that this will be helpful, or at least not without a sense ;-)
On my site, I link pictures from external sites. A lot of the time, the end file names are similar (such as 01.jpg, etc.)
The function seemed to cache the remote image, and use that image over and over, regardless of the file path before the end file name.
To fix that, replace:
list($filename) = explode('?',$finfo['basename']);
$local_filepath = $remoteFolder.$filename;
with:
list($filename) = explode('?',$finfo['basename']);
$file_temp_add = str_replace('/', '_', $finfo['dirname']);
$filename = $file_temp_add."_".$filename;
$local_filepath = $remoteFolder.$filename;
Not a great fix, as it makes the cached file names look crazily unorganized, but your images will now all show if they are using the same file name! :)
This is a fantastic script you've got here. I have it installed on my website, yet i'm having a few troubles.
I am sourcing images from an external URL and not locally. The script is doing it's job perfectly and putting these external images into the 'cache/remote/' folder, however, when viewing the website and what should be the resized images - the images do not show.
The image source is pointing to 'cache/whatever.jpg', and when i checked, there are no resized images being written to the cache folder.
Thanks.
I download the files and try to run on my local machine But its not working. i have WindowsXP XAMMP Latest Server.
Please Help !
Regards
Fahad
i have this code which is not working. please Help!.
<?php
/*
function by Wes Edling .. http://joedesigns.com
feel free to use this in any project, i just ask for a credit in the source code.
a link back to my site would be nice too.
*/
function resize($imagePath,$opts=null){
# start configuration
$cacheFolder = './cache/'; # path to your cache folder, must be writeable by web server
$remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into
$quality = 90; # image quality to use for ImageMagick (0 - 100)
$cache_http_minutes = 20; # cache downloaded http images 20 minutes
$path_to_convert = 'convert'; # this could be something like /usr/bin/convert or /opt/local/share/bin/convert
## you shouldn't need to configure anything else beyond this point
$purl = parse_url($imagePath);
$finfo = pathinfo($imagePath);
$ext = $finfo['extension'];
# check for remote image..
if(isset($purl['scheme']) && $purl['scheme'] == 'http'):
# grab the image, and cache it so we have something to work with..
list($filename) = explode('?',$finfo['basename']);
$local_filepath = $remoteFolder.$filename;
$download_image = true;
if(file_exists($local_filepath)):
if(filemtime($local_filepath) < strtotime(' '.$cache_http_minutes.' minutes')):
$download_image = false;
endif;
endif;
if($download_image == true):
$img = file_get_contents($imagePath);
file_put_contents($local_filepath,$img);
endif;
$imagePath = $local_filepath;
endif;
if(file_exists($imagePath) == false):
$imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath;
if(file_exists($imagePath) == false):
return 'image not found';
endif;
endif;
if(isset($opts['w'])): $w = $opts['w']; endif;
if(isset($opts['h'])): $h = $opts['h']; endif;
$filename = md5_file($imagePath);
if(!empty($w) and !empty($h)):
$newPath = $cacheFolder.$filename.'_w'.$w.'_h'.$h.(isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "").(isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "").'.'.$ext;
elseif(!empty($w)):
$newPath = $cacheFolder.$filename.'_w'.$w.'.'.$ext;
elseif(!empty($h)):
$newPath = $cacheFolder.$filename.'_h'.$h.'.'.$ext;
else:
return false;
endif;
$create = true;
if(file_exists($newPath) == true):
$create = false;
$origFileTime = date("YmdHis",filemtime($imagePath));
$newFileTime = date("YmdHis",filemtime($newPath));
if($newFileTime < $origFileTime):
$create = true;
endif;
endif;
if($create == true):
if(!empty($w) and !empty($h)):
list($width,$height) = getimagesize($imagePath);
$resize = $w;
if($width > $height):
$resize = $w;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = "x".$h;
endif;
else:
$resize = "x".$h;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = $w;
endif;
endif;
if(isset($opts['scale']) && $opts['scale'] == true):
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath;
else:
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -size ".$w."x".$h." xc:".(isset($opts['canvas-color'])?$opts['canvas-color']:"transparent")." swap -gravity center -composite -quality ".$quality." ".$newPath;
endif;
else:
$cmd = $path_to_convert." ".$imagePath." -thumbnail ".(!empty($h) ? 'x':'').$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath;
endif;
$c = exec($cmd);
endif;
# return cache file path
return str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath);
}
?>
So how to implement this image resize script there? I tried but the images wont show. I am confused about the paths and not sure exactly how this script works. Where can i find instructions?
Not just the image resizing, but having all user uploaded images on a site, the same size with cropping is spot on! Thanks!
function.file-put-contents]: failed to open stream: No such file or directory in /virtual/users/e15975-16956/web/resizer/function.resize.php on line 40
image not found' border='0' />
Image cropped & resized by width and height from a remote location.
Any help is appreciated, thanks!
I downloaded the code from your site... But sadly its got some sort of an error..
The imgaes are not being cropped or resized.
I also checked using fire bug, The "src" attribute of the img tag has a value.... but the images dont show on the browser...
Please help me out.
The i tried the code on my server... i works fine... I guess i had some problem running it on xampp. But its a pain if the gallery is huge and has images which weigh more than 600 kb... It takes a long time to cache...
Please let me know if you have a fix
Have you checked your full file path to see if its an accurate path to your file that you are wishing to resize? It looks like that might be your problem.
Sush,
If the images are over 600k, and taking a long time, that is really an issue with ImageMagick, as that is the core library that crunches and resizes images. You could look into optimizing ImageMagick itself, or pre-optimize the images that it is resizing, although once images are cached and resized, it really shouldn't take long to pull them again, its just the one time resize that takes a while.
I hope this helps you both, thanks and happy holidays to everyone
The script works and then stops sometimes.. the src to the resized image get populated but the file does not exist in the cache directory.. does not happen all the time though.. Thank you in advance!
Can this be done?
Van
And if not jpg or gif it shouldent download to remote folder?
Another thing! is there a way to show noimage avaliable if there is not a image on the external address. Thank for this great script. It works great.
Awesome script, got it working fine!
Is there a way it could be modified to clear the cache after a certain amount of time?
The script works fantastic for portrait images, however if I upload a landscape image, the image is resized to the correct width then stretched out of proportion to the correct height - rather than the extra pixels required being added to the canvas.
Is there a fix for this?
However the pixels were not added to the top and bottom of landscape images to increase the canvas size to the height value specified on landscape images.
As you can see no image type suffix. Can this be done using your script??
To those who say this is not working, check your paths are correct and ensure that you have imagemagick compiled on your server. It does work and very well!!
u r great. I implemented ur script in Magento for my custom requirement & u know what it requires no changes u rocks !!
I simply changed:
$ext = $finfo['extension'];
to
$ext = "png";
To force the ImageMagick convert command to output PNG files, regardless of the input type (as long as it is support, which PDF is).
JM2C
In some of the images the black gets dropped. Any ideas on why?
ex:
function in mydomain.com/functionsfolder/
calling page in
mydomain.com/anotherfolder/
images in mydomain.com/folder1/folder2/images/
$cmd = $path_to_convert." ".escapeshellarg($imagePath)." -resize ".$resize." -quality ".$quality." ".$newPath;
Is this slow speed expected due to the original image sizes or is there something I can do to improve this? Or is Imagemagick not the way to go for me, as in, should I already have smaller sizes on the server? Was hoping having one version and quickly serve the user with a smaller version on the fly.
I've been having some problems with the spaces in the images or folders names but i solved it with a single row.
Just put this row:
$imagePath=str_replace(' ', ' ', $imagePath);
Just before:
$purl = parse_url($imagePath);
It should be around line 23 of the php script.
Regars,
Emanuel
function.file-put-contents]: failed to open stream: No such file or directory in D:\www\function.resize.php on line 40
image not found' border='0' />
Script of demo (Online) is really nice but
this is not work for me. Image is not display, please help me.......
Thanks,
Sunil
same problem here as for some guys around...
the script is up, the image file is up as well, however it won't generate the cache files...
i have added this one for the cache folder
$_SERVER['DOCUMENT_ROOT'] . '/cache/';
also, i have created a directory called remote inside the cache folder.. what happens after i run the script, is that it creates the cache of the remote file, but not of the local file stored in /images/
it gives me the it's path
/cache/6e58805ff4763cba92c7e45811fcf4df_w300.jpg
which is unavailable...
any idea on this one?
thanks in advance!
Used this script but not generate the cache file .cache folder permission is 777.Just display below cache image path but not actual image is generated in cache folder
./cache/6e58805ff4763cba92c7e45811fcf4df_w300_h300_sc.jpg
[...]
elseif(!empty($w)):
$cmd = $path_to_convert." ".$imagePath." -thumbnail ".$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath;
else:
$cmd = $path_to_convert." ".$imagePath." -thumbnail x".$h."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath;
endif;
[...]
There should be space added to the top and bottom, however, the script is chopping off the side of the image, which suggests it is scaling the image to the height limit.
Set crop to false and cleared the cache folder.
It downloaded into the remote folder, but didn't convert. I've configured all paths correctly. I've tried both scale and crop settings.
Any suggestions would be extremely appreciated, this is an awesome script for a newbie like me.
I can convert on server side 4000 picture (not on-the-fly, but with small modification your php was great to convert into a folder).
Thx again.mP
http://localhost/folder/example.php?imagefile=dog.jpg&w=300&h=500.
Please help!
Thanks for sharing.
David
any ideas on how to work around this?
Peter Molnar,
Thanks, glad to hear its cranking away on that many pictures :)
Sujit Mourya,
Sounds like you just want to put your resize function inside the <img tag.
The example.php file should help you with the code you'll need
Chris,
Its true, i've been busy with other stuff, but I do check back periodically, and do my best to help. The new script changes by purple pale should help you diagnose the path problems.
Vovanys,
Thanks!
DavidRulz,
Thanks for the comment.. Glad to hear its working for you.
Neil
I'm not too sure about the BLOB from mysql, my guess is to generate the tmp file and then convert it to the blob type into mysql and remove the tmp file. I haven't messed with mysql blob types very often.
Lovinit
Most of the time cache folders don't grow too big, but if you do have a massive amount of images, you'd probably want a cronjob that checks file time and clear out the old stuff..
I just copy pasted the example and the other files, all my dirs are 777 but the function doesn't return anything.
Did somebody had a similar problem?
http://isea.be/isea/example.php
thanks!
My remote folder is writable. And the local files convert without any issue.
Remote files, however, return the following error:
Tried to execute : convert './cache/remote/2934973285_fa4761c982.jpg' -resize '100' -size '100x100' xc:'transparent' swap -gravity center -composite -quality '90' './cache/d41d8cd98f00b204e9800998ecf8427e_w100_h100_cp_sc.jpg', return code: 1, output: Array
(
)
Any ideas as to where I should start looking?
Many thanks!!!!
include 'thepath/function.resize.php'; on top of the php page.
Other than that the issue might be the convert path, but a fault there should still return something.
Make sure ImageMagick is installed and change $path_to_convert = 'convert'; to the proper path, like /usr/bin/convert
Think this will be killer if I can get it working.
1. build imagemagick by doing this or at least verifying what it supports.
USE="-*" emerge -pv imagemagick add the desired things you want to support but make sure to add jpeg if its not highlighted green.
2. change the convert path in function.resize.php from 'convert' to '/usr/bin/convert'.
3. Since Gentoo is bleeding edge make sure to change open short tags to your php.ini to On. they default to off I believe as of php 5.3
or change all the short tages <? to this <?php
4. add a closing "?>" to the function.resize.php file at the very bottom (its missing)
5. if you create the cache and remote folders per the mesage you get and do the above other steps should work great on a gentoo install of php and apache.
cheers
Glen
Thanks
@Adam. In order to remove the top and bottom black border, you'd need to pass "scale" => "true" as an option.
Alternatively, I wanted to comment that you can also force to convert from PNG to JPG (and save space) if you add a line in the script code to capture the jpg extension from $opts and then set it to the output.
I've been using the script on a few websites and it works likea charm, super easy to use and really think this is a great class.
My only problem is when working on a localhost (xampp mac OSX Lion) the code wont work since imagemagick is'nt installed by default.
Do you have any help you could provide with setting up XAMPP - Mac OSX with ImageMagick.
I've been going trough documentation about installation and can't seem to find any dtailed enough to take me trough the whole process.
Thank you for your help
Maybe someone can help with a problem — I'm using the script within a Wordpress sidebar widget... the image is being added to the cache folder fine, but the src on the page is blank.
It's being called like this:
$output .= '<img src="' . resize($photourl,$settings) . '" border="0" alt="' . $linkurl . '" />';
@Phillip, @Edi:
To get imagemagick working on XAMP / MAMP, you'll need to create the binaries, then control the path to the binary files in the exec section of the script.
@Adam: Glad to hear its working for you, and you'll be using it in the future too!
@James: What is your source code look like? Is there an error in the src=''?
<?php passthru('which convert'); ?>
it shows you the $path_to_convert ...
i had some problems with the script on different servers and was wondering why it doesnt work on the second.
the phpsnippet was blank on the 2nd server
thumbs using ur script but example page is not working plz tell me how can we install imagemagick ............
" http://discountsmall.in/affproducts/travel-guru-coupon-codes_86.jpg "
can you please help to find out what is the issue ? and how i can resolve the issue
// $imagePath = urldecode( $imagePath );
$imagePath = str_replace(' ', ' ', urldecode( $imagePath ) );
After that it worked great!
Thanks for the awesome script. I have a little doubt, how do I save the resized image? I need to pass it to further functions for processing. Been trying a lot but couldn't figure out how to save the new resized image in a particular folder.
Please help man!
Rutwick
i use this code but no image display at example.php page so what we do
please help me
i use this code but no image display at example.php page
I checked the script and I noticed that you do not create any image in the cache folder (I set the privileges to 777)
please help me
Array
(
[w] => 300
[h] => 300
[scale] => 1
)
The image shows white bars on both sides. Shouldn't the image just resize without added canvas around it?
if (true == $opts['scale']):
and it works fine now.
Would you please tell how can I get it run on Windows? I have installed ImageMagick.
From the output, it seems the parser did not recognize the function (resize) call: there is a broken link icon followed by ' border='0' />
It is not resizing but cropping as per your demo the image is getting cut it is not resizing/shirking.
I'm using your script for 2 years now. I just notice that it's not resizing nor cropping anymore when I updated my PHP. What seems to be the problem?
thanks!
First of thanks for this great function! I have one question though:
I do see the images come up in the cache folder, and they do load faster because i cloack them from external websites. The only things it doesn't do is resize the images when it saves them. I only specify the height like this:
$settings = array('h'=>200);
What could be the problem here?
Thanks in advance!
The script is awsome, the examples also....
But it doesn't work on my server. I've set the cache it downloads the remote files into ./cache/remote ; but sadly it doesn't make any resized photo... the cache folder is always empty and the function does not return any path :(
Do you have any idea what can be wrong?
Can you tell me what could be the way to solve this.
Thanks
ADD YOUR COMMENT