Page 1 of 1

Resizing multiple images

Posted: Tue Aug 26, 2008 8:08 pm
by tycon
I dont usually post, as I have always been able to find a solution to any code problems I have online. However I have been working for a very long time at this piece of code that uploads multiple images and resizes them. The script calls imageresizer.php several times during a loop to resize images as thumbnails and "fullsize". The only notices/errors I get are for a few variables that dont get used. The problem is the file that are generated are corrupted, meaning that they wont display. I suspect the script does not finish writing the images. I am not sure if this may be a memory issue, I have tried messing with a few parameters in the php.ini file.
code for the main script:

Code: Select all

if ($task==2){
    include('../connect.php');
    $query="INSERT INTO grc_art VALUES ('','$title','$descrip','$cat')";
    mysql_query($query);
    $query="SELECT * FROM grc_art ORDER BY id DESC";
        $alldump=mysql_query($query);
        $lastid=mysql_result($alldump,0,"id");
        $timestamp=date('m_d_y');
        
    for ($i=1; $i<=$max_no_img; $i++){
        $t=$i-1; 
        if ($_FILES['images']['tmp_name'][$t]=='')
            break 1;
        echo "out : $mainimage[$t]";
        $imagename = $timestamp . '_' . 'art' . '_' . $lastid . '_' . $i . '.jpg'; 
        
        copy($_FILES['images']['tmp_name'][$t], $fulldir . $imagename);
        $w=550;
        $img=$fulldir . $imagename;
        include("imageresizer.php");
        
        copy($_FILES['images']['tmp_name'][$t], $thumbdir . $imagename);
        $w=100;
        $img=$thumbdir . $imagename;
        include("imageresizer.php");
        
        $ismain=$mainimage[$t];
        $query="INSERT INTO grc_images VALUES ('','$lastid','$imagename','$ismain')";
        mysql_query($query);
}
mysql_close();
 
echo phpinfo();
}
this is the image resizer:

Code: Select all

$constrain=0;
 
 
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
//echo "$sw";
// image height
$sh = $x[1];
//echo "$sh";
 
if ($percent > 0) {
    // calculate resized height and width if percent is defined
    $percent = $percent * 0.01;
    $w = $sw * $percent;
    $h = $sh * $percent;
} else {
    if (isset ($w) AND !isset ($h)) {
        // autocompute height if only width is set
        $h = (100 / ($sw / $w)) * .01;
        $h = @round ($sh * $h);
    } elseif (isset ($h) AND !isset ($w)) {
        // autocompute width if only height is set
        $w = (100 / ($sh / $h)) * .01;
        $w = @round ($sw * $w);
    } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
        // get the smaller resulting image dimension if both height
        // and width are set and $constrain is also set
        $hx = (100 / ($sw / $w)) * .01;
        $hx = @round ($sh * $hx);
 
        $wx = (100 / ($sh / $h)) * .01;
        $wx = @round ($sw * $wx);
 
        if ($hx < $h) {
            $h = (100 / ($sw / $w)) * .01;
            $h = @round ($sh * $h);
        } else {
            $w = (100 / ($sh / $h)) * .01;
            $w = @round ($sw * $w);
        }
    }
}
 
$im = @ImageCreateFromJPEG ($img) or 
$im = @ImageCreateFromPNG ($img) or 
$im = @ImageCreateFromGIF ($img) or 
$im = false; 
 
if (!$im) {
    readfile ($img);
    echo 'PROBLEM';
} else {
    $thumb = @ImageCreateTrueColor ($w, $h);
    @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
    @ImageJPEG ($thumb, $img);
}
 

Thanks alot

Re: Resizing multiple images

Posted: Wed Aug 27, 2008 12:43 am
by it2051229
but does it work if not multiple?

Re: Resizing multiple images

Posted: Wed Aug 27, 2008 2:34 am
by Christopher
I would first suspect that they are being corrupted when inserted or selected from the database. What kind of field type are you using?

Read:
http://dev.mysql.com/doc/refman/5.0/en/blob.html

Re: Resizing multiple images

Posted: Wed Aug 27, 2008 2:53 am
by onion2k
The reason you're not seeing any errors is because you've suppressed them with @ throughout the script.

Re: Resizing multiple images

Posted: Wed Aug 27, 2008 9:39 am
by tycon
Ok, that is good to know about the ampersand. The images are not being stored in a database as a blob or whatever, they are being stored as files either in the thumbs, or full directory. The database only holds the name of the image, so it can be referenced later. Depending on whether I want to diplay a thumbnail or the full image I just change the directory, as both thumbs and full images have the same name.

I know the upload and copy functions work fine, but the images get corrupted when the resize php file is run. Occasionally the first image gets written sucessfully, but only when it is very small, that is why I think it is some sort of server parameter that is my problem.

Thanks again

Re: Resizing multiple images

Posted: Wed Aug 27, 2008 1:27 pm
by tycon
I should also mention that the imageresizer.php works fine on one file, and it also works fine in a while loop.

Re: Resizing multiple images

Posted: Wed Aug 27, 2008 2:39 pm
by tycon
So after having the images echoed after they are written I was able to see that the script DOES write the images somewhat correctly, that is the browser can always display them. However, when I download the images via ftp, about 10% of them do not display. I have no idea why this would happen, this issue just seems to make less and less sense.

updates to follow...

Re: Resizing multiple images

Posted: Fri Jun 05, 2009 2:10 pm
by adnan3250
For resizing images phpThumb is the best. Cos sometimes in weird situations some "corrupted" images does not get resized if we use general php resizing functions. So using phpThumb is always safe. Here is the details of how to use phpThumb for resizing corrupted images.

Hope that'll save your time and money :)

Re: Resizing multiple images

Posted: Fri Jun 05, 2009 3:16 pm
by mikemike
tycon wrote:Ok, that is good to know about the ampersand.
Not to be picky but:
@ = at symbol
& = ampersand symbol

Re: Resizing multiple images

Posted: Fri Jun 05, 2009 5:04 pm
by Benjamin
Your next steps are to learn proper programming methodologies such as decomposition and handle errors rather than suppressing them.