Page 1 of 2
Resizing Images In Folder
Posted: Mon Nov 21, 2005 7:49 pm
by ajfton
Hey All
I am in REAL NEED of a solution fast - if anyone could help me it would be great.
I am trying to resize image - that in of itself is not the problem. I have tried many different scripts - all work... my problem is this.
1. I don't need a "thumbnail" - I need the script to OVERWRITE the origional.
2. The images are NOT uploaded - its a folder on the server - I need something that will "scan" the folder and look for UP TO 12 images (I know the names of hte images if that helps). The folder will have 1 , 2, 3, up to 12 images in it (sometimes none).
Does anyone know how this can be done....? scan a folder on my server and resize up to 12 images (if any) to a given WIDTH?
In case anyone needs to know, the images will always be named img1.jpg , img2,jpg, etcc.. up to img12.jpg and they will all be in the same folder.
I need somethign I can call when needed to scan the folder and resize the images in it to something smaller..
if anyone could help me it would be great. I am no expert in PHP, my knowlege is elsewhere. If anyone could help me with some code or explain to me how I could make changes to one of the many thumbnail scripts out there... anything to get this fixed.
Posted: Tue Nov 22, 2005 4:17 am
by Grim...
One solution. FAST, as requested
Code: Select all
<?php
$dir = $_SERVER["DOCUMENT_ROOT"]."directory/where/your/photos/are";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$src_name = $dir."/".$file;
$dest_name = $dir."/".$file;
if ($src = imagecreatefromjpeg($src_name))
{
continue;
}
$image_width = imagesx($src);
$image_height = imagesy($src);
$h = 100;
$scale = $image_height / 100;
$w = ceil($image_width / $scale);
//$h and $w are your height and width of the new image. Here I scale width according to height
$dest = imagecreatetruecolor($w,$h);
imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
unlink($src_name);
imagejpeg($dest, $dest_name, 50);
imagedestroy($dest);
}
}
closedir($dh);
}
?>
thanks...
Posted: Tue Nov 22, 2005 10:24 am
by ajfton
Thanks....
I willl try it now and see if it works...
Not working...
Posted: Tue Nov 22, 2005 10:50 am
by ajfton
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
It does'nt seam to be working - I am not getting any error message , the script just runs but nothing happens - the images are the same (not resized).
Any idea why this may be - did I do something wrong or not call the script right?
Code: Select all
<?php
$dir = "/var/www/html/test";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$src_name = $dir."/".$file;
$dest_name = $dir."/".$file;
if ($src = imagecreatefromjpeg($src_name))
{
continue;
}
$image_width = imagesx($src);
$image_height = imagesy($src);
$h = 100;
$scale = $image_height / 100;
$w = ceil($image_width / $scale);
$dest = imagecreatetruecolor($w,$h);
imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
unlink($src_name);
imagejpeg($dest, $dest_name, 50);
imagedestroy($dest);
}
}
closedir($dh);
}
?>
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Tue Nov 22, 2005 11:54 am
by Grim...
The folder needs CDMOD permissions of 0775.
Simplest way to do this: Browse to it in your FTP software, right click on the folder, and set all the permissions to 'on'.
If that doesn't help, replace your code with:
Code: Select all
$dir = $_SERVER["DOCUMENT_ROOT"]."directory/where/your/photos/are";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$src_name = $dir."/".$file;
$dest_name = $dir."/".$file;
if ($src = imagecreatefromjpeg($src_name))
{
print "Cannot create image from ".$src_name."<br /><br />";
continue;
}
$image_width = imagesx($src);
$image_height = imagesy($src);
$h = 100;
$scale = $image_height / 100;
$w = ceil($image_width / $scale);
//$h and $w are your height and width of the new image. Here I scale width according to height
$dest = imagecreatetruecolor($w,$h);
imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
unlink($src_name);
imagejpeg($dest, $dest_name, 50);
imagedestroy($dest);
}
}
closedir($dh);
}
?>
will try to new code...
Posted: Tue Nov 22, 2005 11:56 am
by ajfton
I fured that, that folder was 0775 - I will try the new code you gave me.
Posted: Tue Nov 22, 2005 12:01 pm
by Grim...
Sorry, I made a mistake.
Remove the whole
Code: Select all
if ($src = imagecreatefromjpeg($src_name))
statement, and see if that works.
Posted: Tue Nov 22, 2005 12:03 pm
by Grim...
Better code would be:
Code: Select all
<?php
$dir = $_SERVER["DOCUMENT_ROOT"]."directory/where/your/photos/are";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$src_name = $dir."/".$file;
$dest_name = $dir."/".$file;
if ($src = imagecreatefromjpeg($src_name))
{
$image_width = imagesx($src);
$image_height = imagesy($src);
$h = 100;
$scale = $image_height / 100;
$w = ceil($image_width / $scale);
//$h and $w are your height and width of the new image. Here I scale width according to height
$dest = imagecreatetruecolor($w,$h);
imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
unlink($src_name);
imagejpeg($dest, $dest_name, 50);
imagedestroy($dest);
}
else
{
print "Cannot create image from ".$src_name."<br /><br />";
continue;
}
}
closedir($dh);
}
?>
Once it's working nicely, take out the print statement.
Success... Mostly :-)
Posted: Tue Nov 22, 2005 12:19 pm
by ajfton
Success... Mostly
Hey Grim - thanks so much for you help.
It is working (almost perfectly) - it gives the error message:
Cannot create image from /var/www/html/test/.
Cannot create image from /var/www/html/test/..
But it does actually resize the images - if I go look in the folder hte images were indeed resized as they should have been.
Can this be easily changed resize by WIDTH rather than hieght? Sorry to be a pain - basically the folder will have
UP TO 12 images, any mumber of them that
MAY or
MAY NOT be too wide (y
ou never know as the folders contents change from time to time) - so I could resize by width instead of height that would totally solve my problem.
Again.. thanks so much for your help. You really did help me out here.
Figured It Out..
Posted: Tue Nov 22, 2005 12:29 pm
by ajfton
Hey
I managed to figure out how to change it from Height to Width already
AJ
One last thing...
Posted: Tue Nov 22, 2005 1:14 pm
by ajfton
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Below is the code that I have now it IS working to resize images in the folder - however there is a little problem...
It resizing images no matter what - it's not a MAX Width - it's resizing the images even if they are smaller than the set width...
SO images that were only 100x100 are being resized to a bigger image.
Right now the code is working and resizing images to 400 wide (and whatever height it may - keeping the aspect ratio the same).
Does any one know how I can change this code so it will get the width of the image and only resize it IF it needs to be (if the image is wider than the set width).
Perhaps using the "getimagesize" command and some kind of IF statment - keeping in mind there may be multple images - up to 12 - so it would have to do this for each, I think anyway...
Code: Select all
<?php
$dir = "/var/www/html/test";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$src_name = $dir."/".$file;
$dest_name = $dir."/".$file;
if ($src = imagecreatefromjpeg($src_name))
{
$image_width = imagesx($src);
$image_height = imagesy($src);
$w = 400;
$scale = $image_width / 400;
$h = ceil($image_height / $scale);
$dest = imagecreatetruecolor($w,$h);
imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
unlink($src_name);
imagejpeg($dest, $dest_name, 70);
imagedestroy($dest);
}
else
{
print "Cannot create image from ".$src_name."<br /><br />";
continue;
}
}
closedir($dh);
}
?>
Jcart | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Tue Nov 22, 2005 2:34 pm
by Grim...
Code: Select all
<?php
$dir = "/var/www/html/test";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$src_name = $dir."/".$file;
$dest_name = $dir."/".$file;
if ($src = imagecreatefromjpeg($src_name))
{
$image_width = imagesx($src);
$image_height = imagesy($src);
if ($image_width < 400)
{
continue;
}
$w = 400;
$scale = $image_width / 400;
$h = ceil($image_height / $scale);
$dest = imagecreatetruecolor($w,$h);
imagecopyresized($dest, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
unlink($src_name);
imagejpeg($dest, $dest_name, 70);
imagedestroy($dest);
}
}
closedir($dh);
}
?>
Enjoy

Posted: Tue Nov 22, 2005 3:06 pm
by JAM
Moved to a better section of the board. To me, the original post wasn't a question about a specific problem or jugling ideas.
It's just a quick atempt on a solution, with code pasted back and forth...

Posted: Tue Nov 22, 2005 4:34 pm
by Grim...
Would have been over a lot quicker if I hadn't made such a big mistake in my first post

Thanks... works perfect
Posted: Tue Nov 22, 2005 6:52 pm
by ajfton
Hey Grim...
Just wanted to say THANK YOU (very very much).
It worked and you were a big help.
I'm not the best at PHP yet but learning (got some books today)... but was in kind of jam to get something fixed...
I just went your Amazon wishlist thing and purchased somethign for you, to say thanks for helping
"Aliens: Complete Illustrated Screenplay", it was the first thing on the list.
AJ