problem with copy ans resize
Posted: Thu Oct 14, 2010 7:10 am
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!
***********************************************************************************************
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);
}