Massive picture resizing loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Massive picture resizing loop

Post 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?
Last edited by s.dot on Tue Aug 09, 2005 8:13 pm, edited 2 times in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

kinda hard to tell if it'll overwrite the file since I don't see the code.. so Swami says.. yes.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

} 

?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

lmao I wrote that part a LONG time ago.. should've just used a strtolower() eh? ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_match('#jpe?g#i',$whatever)
that's the equivalent.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

755 or 757 should do it.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The pictures are in hundreds of directories, doing that manually would be.... extremely time consuming.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply