SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is this 'Division by zero' problem while uploading photo ?

    Hi guys

    Wondering how many of you heard about this division by zero problem. I am using a script to resize and update photos to database, and have used the same script for a lot of different pages, and never had any problem.

    This time I tried to modify the same to store two different sizes of images in the database (resizing the same image basically), and this problem is coming. It tells me
    Code:
    Warning: Division by zero in ... /photo-added.php on line ...
    , and it comes with the variable which I use to store the image data
    PHP Code:
    $filedata and $filedata_s in this exemple 
    (means all those strange characters in the database), and just before those strange characters.

    Here goes that part of the script that's creating the weird error ...
    PHP Code:
    $uploadfile=$_FILES['auth_img']['tmp_name'];
    $uploadname=$_FILES['auth_img']['name'];
    $uploadtype=$_FILES['auth_img']['type'];

    $src ImageCreateFromString(file_get_contents($uploadfile)); 
    $new_width 350// set max limit for width for new image 
    $new_height 350// set max limit for height for new image 
    $old_width ImageSx($src); //find width of old file 
    $old_height ImageSy($src); //find height of old file 
        
    if (($old_height $new_height) || ($old_width $new_width)) {
    if (
    $old_height $old_width)
    {  
    $new_width round(($new_height $old_height) * $old_width); }
    if (
    $old_width $old_height)
    $new_height round(($new_width $old_width) * $old_height); }
    } else { 
    $new_width $old_width;
    $new_height $old_height;
    }
            
    $dst ImageCreateTrueColor($new_width,$new_height);
    ImageCopyResampled($dst$src,0,0,0,0,$new_width,$new_height,$old_width,$old_height);     
    $quality 75 
        
    ob_start(); 
    if (
    $uploadtype==image/gif
    {
    Imagegif($dst$uploadfile);} 
    elseif (
    $uploadtype==image/png
    {
    Imagepng($dst$uploadfile);} else { 
    Imagejpeg($dst''$quality); } 
    $filedata ob_get_clean(); 
    Imagedestroy($dst); 
    $uploadsize strlen($filedata);
    $img_height=$new_height;
    $img_width=$new_width;
        
    // data for the second table, thumbnails
    $new_width_s 100// set max limit for width for new image 
    $new_height_s 100// set max limit for height for new image
            
    if (($old_height $new_height_s) || ($old_width $new_width_s)) {
    if (
    $old_height $old_width)
    {  
    $new_width_s round(($new_height_s $old_height) * $old_width); }
    if (
    $old_width $old_height)
    $new_height_s round(($new_width_s $old_width) * $old_height); }
    } else { 
    $new_width_s $old_width;
    $new_height_s $old_height;
    }

    $dst_s ImageCreateTrueColor($new_width_s,$new_height_s); ImageCopyResampled($dst_s$src,0,0,0,0,$new_width_s,$new_height_s,$old_width,$old_height); // copy old image into new image resized 

    Imagedestroy($src); 
        
    $quality 75 ;    
    ob_start(); 
    if (
    $uploadtype==image/gif
    {
    Imagegif($dst_s$uploadfile);} 
    elseif (
    $uploadtype==image/png
    {
    Imagepng($dst_s$uploadfile);} else { 
    Imagejpeg($dst_s''$quality); } 
    $filedata_s ob_get_clean(); 
    Imagedestroy($dst_s); 
    $uploadsize_s strlen($filedata_s);
    $img_height_s=$new_height_s;
    $img_width_s=$new_width_s;

    $filedata=addslashes($filedata);
    $filedata_s=addslashes($filedata_s); 
    Any tips, anybody?
    Thanks.

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Warning: Division by zero in ... /photo-added.php on line ...
    ...what line number? And what is the code on that line?

  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well Mark, this is just after the ob_start(), line number 80 and 82. Line if and else if

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have (in 2 places):
    $uploadtype==image/gif .. and $uploadtype==image/png
    those should be:
    $uploadtype=='image/gif' .. and $uploadtype=='image/png'

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Mark. I try that. But strange that the same code was working while uploading / resizing the image to one size only.

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great Mark. That's working now. U'r a genious ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •