Jump to content

Image upload *was* working, now isn't, a solution?


tjhilder

Recommended Posts

Hi, this script used to work, but i'm not sure why it isn't now.

 

this script basicly uploads an image, makes a thumbnail of it, then uploads some info into a database.

<?php
            define ("MAX_SIZE","100");
            define ("WIDTH","150");
            define ("HEIGHT","100");

            function make_thumb($img_name, $filename, $new_w, $new_h)
            {
               $ext = getExtension($img_name);
               if(!strcmp("jpg", $ext) || !strcmp("jpeg", $ext))
               {
                  $src_img = imagecreatefromjpeg($img_name);
               }
               if(!strcmp("png", $ext))
               {
                  $src_img = imagecreatefrompng($img_name);
               }
               if(!strcmp("gif", $ext))
               {
                  $src_img = imagecreatefromgif($img_name);
               }

               $old_x = imageSX($src_img);
               $old_y = imageSY($src_img);

               $ratio1 = $old_x / $new_w;
               $ratio2 = $old_y / $new_h;
               if($ratio1 > $ratio2)
               {
                  $thumb_w = $new_w;
                  $thumb_h = $old_y / $ratio1;
               }
               else
               {
                  $thumb_h = $new_h;
                  $thumb_w = $old_x / $ratio2;
               }

               $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);

               imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);

               if(!strcmp("png", $ext))
               {
                  imagepng($dst_img, $filename);
               }
               else
               {
                  imagejpeg($dst_img, $filename);
               }

               imagedestroy($dst_img);
               imagedestroy($src_img);
            }

            function getExtension($str)
            {
               $i = strrpos($str, ".");
               if (!$i)
               {
                  return "";
               }
               $l = strlen($str) - $i;
               $ext = substr($str, $i+1, $l);
               return $ext;
            }

            $errors = 0;

            if(isset($_POST['submit']))
            {
               $image = $_FILES['image']['name'];
               if($image)
               {
                  $filename = stripslashes($_FILES['image']['name']);

                  $extension = getExtension($filename);
                  $extension = strtolower($extension);

                  if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
                  {
                     $intel[] = "Error: Wrong Extention, can only be jpg, jpeg, png or gif.";;
                     $errors=1;
                  }
                  else
                  {
                     $size = getimagesize($_FILES['image']['tmp_name']);
                     $sizekb = filesize($_FILES['image']['tmp_name']);

                     if ($sizekb > MAX_SIZE * 10240)
                     {
                        $intel[] = "Error: File is bigger than 10mb";
                        $errors = 1;
                     }

                     $image_name = $_FILES['image']['name'];


                     $newname = $config_gallery_dir . $image_name;
                     $copied = copy($_FILES['image']['tmp_name'], $newname);

                     if (!$copied)
                     {
                        $intel[] = "Error: Your image was not uploaded.";
                        $intel[] = "Error: Your thumbnail was not created.";
                     }
                     else
                     {
                        $thumb_name = $config_thumbnail_dir . $image_name;
                        $thumb = make_thumb($newname, $thumb_name, WIDTH, HEIGHT);

                        $intel[] = "Success! Your image was uploaded.";
                        $intel[] = "Success! Your thumbnail was created.";

                     }
                  }
               }
               $insert_query = "INSERT INTO " . $config_gallery_mysql_tbl . " (image, image_ext) VALUES ('" . $_FILES["image"]["name"] . "', '" . $extension . "')";
               if($insert_result = mysql_query($insert_query))
               {
                  $intel[] = "Success! the database has been updated.";
               }
               else
               {
                  $intel[] = "Error! the database was not updated.";
               }
               if(isset($intel))
               {
                  echo "<ul>\n";
                  foreach($intel AS $value)
                  {
                     echo "<li>" . $value . "</li>\n";
                  }
                  echo "</ul>\n";
               }
            }
            else
            {
?>
         <form action="?action=add" method="post" enctype="multipart/form-data">
         <table cellspacing="0" cellpadding="0" border="0" id="admin-table">
            <tr>
               <td class="admin-left">Upload an image</td>
               <td class="admin-right"><input type="file" name="image" size="50" /></td>
            </tr>
            <tr>
               <td class="admin-left"> </td>
               <td class="admin-right"><input type="submit" name="submit" value="Submit" /></td>
            </tr>
         </table>
         </form>

 

But when I try to run this script, I get these errors:

 

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 157: Undefined variable: src_img

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 157: imagesx(): supplied argument is not a valid Image resource

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 158: Undefined variable: src_img

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 158: imagesy(): supplied argument is not a valid Image resource

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 170: Division by zero

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 173: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 175: Undefined variable: src_img

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 175: imagecopyresampled(): supplied argument is not a valid Image resource

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 183: imagejpeg(): supplied argument is not a valid Image resource

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 186: imagedestroy(): supplied argument is not a valid Image resource

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 187: Undefined variable: src_img

An error occurred in script '/home/tjhilder/public_html/faux/cp/gallery.php' on line 187: imagedestroy(): supplied argument is not a valid Image resource

 

$config_gallery_dir and $config_thumbnail_dir are located in an external config.inc.php file and as far as I know are not pointing to the wrong area because it uploads the image fine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.