Page 1 of 2

Massive picture resizing loop

Posted: Tue Aug 09, 2005 6:43 pm
by s.dot
I'm about to resize thousands of pictures using a script, so I want to make sure it's going to do it right before I go and <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurfer</span> things up =)

Code: Select all

<?
include 'createthumb.inc.php'; // this file resizes a picture to specivied values

/* Set up query to select all main pictures */
$result = mysql_query("SELECT username, filename FROM pictures");

/* Loop through the results and perform resizing if width OR height > 450 */
while($array = mysql_fetch_assoc($result))
{
	/* Get picture dimensions */
	$size = getimagesize("http://www.domain.com/uploads/".$array['username']."/".$array['filename']."");
	
	/* Check to see if the picture NEEDS resized */
	if(($size['0'] > 450) || ($size['1'] > 450))
	{
		/* this function's first parameter is the name of the original picture to be resized, the second paramter is the name of the resized picture */
		createthumb("uploads/".$array['username']."/".$array['filename']."","uploads/".$array['username']."/".$array['filename']."",450,450);
	}
}
Questions I have:
1) In the createthumb() function, will the newly resized picture overwrite (notice both parameters are the same path, same file name) the old one? If not, how can I change it so that it does overwrite the old one.

2) Have I missed anything I should be aware of before running this script on 2 gigs of pictures?

Posted: Tue Aug 09, 2005 6:54 pm
by feyd
kinda hard to tell if it'll overwrite the file since I don't see the code.. so Swami says.. yes.

Posted: Tue Aug 09, 2005 6:58 pm
by s.dot
Here's the code inside createthumb.inc.php

Code: Select all

<?

function createthumb($name,$filename,$new_w,$new_h){

    global $gd2;

    $system=explode(".",$name);

    if (preg_match("/jpg|jpeg|JPG|JPEG|Jpg|JpG|jPG|JPg|JPEg|JPeg|Jpeg|jPEG|jpEG|jPeG|JpEg|JPEg|JpeG|jpEg|jpeG/",$system[1])){

        $src_img=imagecreatefromjpeg($name);

    }

    if (preg_match("/png|PNG|pNg|Png|pNG|PnG/",$system[1])){

        $src_img=imagecreatefrompng($name);

    } 

    $old_x=imageSX($src_img);

    $old_y=imageSY($src_img);

    if ($old_x > $old_y) {

        $thumb_w=$new_w;

        $thumb_h=$old_y*($new_h/$old_x);

    }

    if ($old_x < $old_y) {

        $thumb_w=$old_x*($new_w/$old_y);

        $thumb_h=$new_h;

    }

    if ($old_x == $old_y) {

        $thumb_w=$new_w;

        $thumb_h=$new_h;

    } 

        $dst_img=imagecreatetruecolor($thumb_w,$thumb_h);

        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);


    if (preg_match("/png|PNG|pNg|Png|pNG|PnG/",$system[1])){

        imagepng($dst_img,$filename);

    } else {

        imagejpeg($dst_img,$filename);

    }

    imagedestroy($dst_img);

    imagedestroy($src_img);

} 

?>

Posted: Tue Aug 09, 2005 7:04 pm
by feyd
now, yes. I can definitely say it will overwrite any file that may be in place where you are sending it to.

I can also say, the writer of the function is not so good with regex.. 'cause damn:

Code: Select all

preg_match("/jpg|jpeg|JPG|JPEG|Jpg|JpG|jPG|JPg|JPEg|JPeg|Jpeg|jPEG|jpEG|jPeG|JpEg|JPEg|JpeG|jpEg|jpeG/",$system[1])
I greatly apologize if you wrote it, but dang that's just messed up.

Posted: Tue Aug 09, 2005 7:07 pm
by s.dot
lmao I wrote that part a LONG time ago.. should've just used a strtolower() eh? ;)

Posted: Tue Aug 09, 2005 7:09 pm
by feyd

Code: Select all

preg_match('#jpe?g#i',$whatever)
that's the equivalent.

Posted: Tue Aug 09, 2005 7:10 pm
by feyd
to be careful, of failures and such.. I'd suggest sending the files to a seperate folder.. you can always replace the existing sets with that new one later..

Posted: Tue Aug 09, 2005 7:14 pm
by s.dot
No, I think you're lying :lol: (says the person foreign to regex)

From what I gather, ? represents an optional character and # matches lower or upper

Posted: Tue Aug 09, 2005 7:15 pm
by feyd
you got the ?, but # is purely a start and end marker.. it can be any symbol.. I just happen to use # because it's a bit more rare to see that in the things I'm using as a character in the pattern string..

Posted: Tue Aug 09, 2005 7:27 pm
by s.dot
I tested it out on one picture and it did not work. Could it be the filepermissions? Right now I have the file permissions as 644

Posted: Tue Aug 09, 2005 7:29 pm
by feyd
755 or 757 should do it.

Posted: Tue Aug 09, 2005 7:41 pm
by s.dot
Don't know if that was the cause (could've been) but I also forgot to include the database connect info 8-| anywho works now. Thanks.

Posted: Tue Aug 09, 2005 8:15 pm
by s.dot
Weird.. now that I tried to perform the mass operation, the resizing did not work.

Code: Select all

include 'important.php';
include 'createthumb.inc.php'; // this file resizes a picture to specivied values

/* Set up query to select all main pictures */
$result = mysql_query("SELECT username, filename FROM pictures");

/* Loop through the results and perform resizing if width OR height > 450 */
while($array = mysql_fetch_assoc($result))
{
	/* Get picture dimensions */
	$size = getimagesize("http://www.showmypro.com/uploads/".$array['username']."/".$array['filename']."");
	
	/* Check to see if the picture NEEDS resized */
	if(($size['0'] > 450) || ($size['1'] > 450))
	{
		/* this function's first parameter is the picture to be resized, the second paramter is where to place the resized picture */
		chmod("/home/scottttt/public_html/uploads/".$array['username']."/".$array['filename']."", 0777);
		createthumb("uploads/".$array['username']."/".$array['filename']."","uploads/".$array['username']."/".$array['filename']."",450,450);
		chmod("/home/scottttt/public_html/uploads/".$array['username']."/".$array['filename']."", 0644);
		echo "done.<BR>";
	}
}
echo "done";
I run this script... and it loops through it and displays a bunch of "done"s.. as I told the script to do. However, I run it again (and it shouldn't show any "done's" because the pictures should've been resized. But it goes through and does it again.

I look at the pictures and none of them are resized.

Are my CHMOD commands correct?

Posted: Tue Aug 09, 2005 8:30 pm
by feyd
could just do it locally, then upload it :evil:

why not just do the chmod outside of php, and reset it after the run completes?

Posted: Tue Aug 09, 2005 8:32 pm
by s.dot
The pictures are in hundreds of directories, doing that manually would be.... extremely time consuming.