Resizing Images In Folder
Moderator: General Moderators
Resizing Images In Folder
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.
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.
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);
}
?>Not working...
Jcart | Please use
Jcart | Please use
Code: Select all
andCode: 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
andCode: 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]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:
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);
}
?>
Last edited by Grim... on Tue Nov 22, 2005 11:57 am, edited 1 time in total.
will try to new code...
I fured that, that folder was 0775 - I will try the new code you gave me.
Sorry, I made a mistake.
Remove the whole
Code: Select all
if ($src = imagecreatefromjpeg($src_name))Better code would be:
Once it's working nicely, take out the print statement.
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);
}
?>Success... Mostly :-)
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 (you 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.
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 (you 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..
Hey
I managed to figure out how to change it from Height to Width already
AJ
I managed to figure out how to change it from Height to Width already
AJ
One last thing...
Jcart | Please use
Jcart | Please use
Code: Select all
andCode: 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
andCode: 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]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);
}
?>Thanks... works perfect
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
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