problem with copy ans resize

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

Post Reply
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

problem with copy ans resize

Post by vaalsh »

Question from newbie... mysql/php
Simple script:
for every file in directory, make record in DB, create copy of the file, put to another folder, resize it 2 times and put to 2 other folders.

Script works fine for 20-40 files, when it more than 50, it stops with no errors...

Really appreciate any help!

***********************************************************************************************

Code: Select all

if ($handle = opendir('.')) {
    while (false !== ($file_name = readdir($handle))) {
        if ($file_name  != "." && $file_name  != ".." && $file_name ) {
//==========================================================================
		$crnt_id++; // CARENT ID = NAME OF THE FILE
		$count++;
		if($lang=="english"){
			mysql_query("INSERT INTO ".$cat."(id, eng_name) VALUES ('".$crnt_id."', '".$name_in."')");
		}else{
			mysql_query("INSERT INTO ".$cat."(id, name) VALUES ('".$crnt_id."', '".$name_in."')");
		}
		$result = mysql_query($query);
		
		if (($result)||(mysql_errno == 0)){
	//================== PUT FILES TO FOLDER 'BIG' ==================================
			$copy = copy($file_name,'../'.$cat.'/Big/'.$crnt_id.'.jpg');
			
		//=============check if successfully copied========================================
			if($copy){
				echo "$file_name - uploaded sucessfully!  -  ".$count."<br>";
				
			 }else{
				echo "$file_name - could not be uploaded!<br>";
				exit("     ".$count);
			}
			$filename='../'.$cat.'/Big/'.$crnt_id.'.jpg';
		
			resizeImage($filename,'Mid',$cat,$crnt_id,470,460);// RESIZE AND PUT TO FOLDER 'MID'

			resizeImage($filename,'Thumbs',$cat,$crnt_id,140,0);//RESIZE AND PUT TO FOLDER 'THUMBS'

		}
	}
}
}
closedir($handle); 
mysql_close();

function resizeImage($name,$mod,$ctg,$id,$w,$l){

	list($width_orig, $height_orig) = getimagesize($name);

	if($mod=='Mid'){
		if($width_orig>$height_orig){
			$width=$w;
			$ratio=$width_orig/$w;
			$height=$height_orig/$ratio;
		}else{
			$height=$l;
			$ratio=$height_orig/$l;
			$width=$width_orig/$ratio;
		}
	}else{
			$width=140;
			$ratio=$width_orig/140;
			$height=$height_orig/$ratio;
	}	
	// Resample
	$image_p = imagecreatetruecolor($width, $height);
	$image = imagecreatefromjpeg($name);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
	
	// Output
	imagejpeg($image_p,'../'.$ctg.'/'.$mod.'/'.$id.'.jpg');
	
    imagedestroy($image_p);
    imagedestroy($image);
 }
Last edited by Benjamin on Thu Oct 14, 2010 7:27 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: problem with copy ans resize

Post by twinedev »

When it runs for so many, then quits for more, the first thing that pops into my mind is that the script is timing out, going past what settings are on the server for how much time a script can execute.

If that was the case, here is something I would try:

1. Figure out how long the script can execute (eg, 30 seconds).

2. Have it build the database of the original images.

3. Go ahead and do your normal thing, where it will time out, BUT on the output, if there are still files that need processed, have it issue a meta refresh on the page so that it will call itself again after the time out time (plus a little buffer)

This way, it will index them, process say 30 of them and time out, but your browser after 45 seconds refreshes itself, it goes down that list, looking for files who other 2 copies don't exist and continues where it left off (ie, don't have ti redo files already processed)

When it runs for the last time and sees all files are in place, it will not include the META refresh, and will simply display a "DONE" type message.

-Greg
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: problem with copy ans resize

Post by vaalsh »

Thank You Greg, for Your help, it works now!

Val.

twinedev wrote:When it runs for so many, then quits for more, the first thing that pops into my mind is that the script is timing out, going past what settings are on the server for how much time a script can execute.

If that was the case, here is something I would try:

1. Figure out how long the script can execute (eg, 30 seconds).

2. Have it build the database of the original images.

3. Go ahead and do your normal thing, where it will time out, BUT on the output, if there are still files that need processed, have it issue a meta refresh on the page so that it will call itself again after the time out time (plus a little buffer)

This way, it will index them, process say 30 of them and time out, but your browser after 45 seconds refreshes itself, it goes down that list, looking for files who other 2 copies don't exist and continues where it left off (ie, don't have ti redo files already processed)

When it runs for the last time and sees all files are in place, it will not include the META refresh, and will simply display a "DONE" type message.

-Greg
Post Reply