Resizing Images In Folder

Looking for volunteers to join your project? Need help with a script but can't afford to pay? Want to offer your services as a volunteer to build up your portfolio? This is the place for you...

Moderator: General Moderators

ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

Resizing Images In Folder

Post 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.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
}

?>
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

thanks...

Post by ajfton »

Thanks....

I willl try it now and see if it works...
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

Not working...

Post by ajfton »

Jcart | Please use

Code: Select all

and

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

and

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]
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
}

?>
Last edited by Grim... on Tue Nov 22, 2005 11:57 am, edited 1 time in total.
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

will try to new code...

Post by ajfton »

I fured that, that folder was 0775 - I will try the new code you gave me.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

:lol:
Sorry, I made a mistake.

Remove the whole

Code: Select all

if ($src = imagecreatefromjpeg($src_name))
statement, and see if that works.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

Success... Mostly :-)

Post 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 (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.
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

Figured It Out..

Post by ajfton »

Hey

I managed to figure out how to change it from Height to Width already

AJ
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

One last thing...

Post by ajfton »

Jcart | Please use

Code: Select all

and

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

and

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]
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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 :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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... :roll:
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Would have been over a lot quicker if I hadn't made such a big mistake in my first post :P
ajfton
Forum Newbie
Posts: 13
Joined: Mon Oct 25, 2004 6:07 am
Location: Fredericton, NB, Canada

Thanks... works perfect

Post 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
Post Reply