Resizing multiple images

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
tycon
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 7:51 pm

Resizing multiple images

Post 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
Last edited by Benjamin on Fri Jun 05, 2009 5:02 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Resizing multiple images

Post by it2051229 »

but does it work if not multiple?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Resizing multiple images

Post 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
(#10850)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Resizing multiple images

Post by onion2k »

The reason you're not seeing any errors is because you've suppressed them with @ throughout the script.
tycon
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 7:51 pm

Re: Resizing multiple images

Post 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
tycon
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 7:51 pm

Re: Resizing multiple images

Post by tycon »

I should also mention that the imageresizer.php works fine on one file, and it also works fine in a while loop.
tycon
Forum Newbie
Posts: 4
Joined: Tue Aug 26, 2008 7:51 pm

Re: Resizing multiple images

Post 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...
adnan3250
Forum Newbie
Posts: 1
Joined: Fri Jun 05, 2009 2:00 pm

Re: Resizing multiple images

Post 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 :)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Resizing multiple images

Post by mikemike »

tycon wrote:Ok, that is good to know about the ampersand.
Not to be picky but:
@ = at symbol
& = ampersand symbol
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Resizing multiple images

Post by Benjamin »

Your next steps are to learn proper programming methodologies such as decomposition and handle errors rather than suppressing them.
Post Reply