We need a GOOD image script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidppppppppppp
Forum Newbie
Posts: 13
Joined: Mon Jul 03, 2006 9:31 pm

We need a GOOD image script

Post by davidppppppppppp »

Anyone know of a good image upload script?

By good I mean one that is actually useful and works. That may seem silly but most image scripts I've looked at are rather namby_pamby.

To me a useful image script will:
  • Be incorporated into a form where a user can include other information as well such as: image_description, image_category, etc.
  • The script uploads the orginal image saving it to a folder and creating a thumbnail (keeping the same name) and uploading it into a seperate folder on the server.
  • The script must have a max_image_size included and working. It must check this first - even before it checks to see if it's an image file being uploaded or not. It doesn't make sense to make someone wait for the script to run only to find that the picture didn't upload because it was too big. So it checks this first and if it's too big a message is returned right off saying so.
  • There will be hidden files that the script will automatically populate with the original image information such as: image_width, image_height, image_type, image_location (on server) and takes these and the image_name and puts it into the database along with the other user information.
An image script that doesn't create a thumbnail and inserts the name and location of both thumbnail and original image into a database isn't very useful. Not to me anyway.

David
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

In general you won't find a script that does exactly what you want. You'll either have to find one that does almost what you want and modify it, or else write one from scratch. Fortunately image uploads and generating thumbnails is pretty easy so you shouldn't have any problem doing so.
davidppppppppppp
Forum Newbie
Posts: 13
Joined: Mon Jul 03, 2006 9:31 pm

Post by davidppppppppppp »

How can I merge this two scripts together:

Image Upload script:

Code: Select all

<?php 
$idir = "pics/";   // Path To Images Directory 
$tdir = "pics/thumbs/";   // Path To Thumbnails Directory 
$twidth = "125";   // Maximum Width For Thumbnail Images 
$theight = "100";   // Maximum Height For Thumbnail Images 

if (!isset($_GET['subpage'])) {   // Image Upload Form Below   ?> 

  <form method="post" action="addphoto.php?subpage=upload" enctype="multipart/form-data"> 
   File:<br />
   <input type="file" name="imagefile" class="form"> 
  <br /><br /> 
  <input name="submit" type="submit" value="Sumbit" class="form">  <input type="reset" value="Clear" class="form"> 
  </form> 

<? } else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script 
  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use 
  if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { 
    $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
    $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 
    if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 
      print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image 
      $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From 
      $currwidth = imagesx($simg);   // Current Image Width 
      $currheight = imagesy($simg);   // Current Image Height 
      if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
         $zoom = $twidth / $currheight;   // Length Ratio For Width 
         $newheight = $theight;   // Height Is Equal To Max Height 
         $newwidth = $currwidth * $zoom;   // Creates The New Width 
      } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
        $zoom = $twidth / $currwidth;   // Length Ratio For Height 
        $newwidth = $twidth;   // Width Is Equal To Max Width 
        $newheight = $currheight * $zoom;   // Creates The New Height 
      } 
      $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
      $palsize = ImageColorsTotal($simg); 
      for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
      } 
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
      imagejpeg($dimg, "$tdir" . $url);   // Saving The Image 
      imagedestroy($simg);   // Destroying The Temporary Image 
      imagedestroy($dimg);   // Destroying The Other Temporary Image 
      print 'Image thumbnail created successfully.';   // Resize successful 
    } else { 
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed 
    } 
  } else { 
    print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong 
    print $file_ext;   // Show The Invalid File's Extention 
    print '.</font>'; 
  } 
}
?>
with this form:

Code: Select all

<form action="insert_item.php" method="post">

<p>

Item Name:<br>
<input type="text" name="item_name" size="45" value="" ">

<br><br>

Item description:<br>
<textarea rows="10" name="item_desc" cols="35" wrap=virtual></textarea>

<br><br>

Item Price: &nbsp;Example: 20.00 (do not use $-sign)<br>
<input type="text" name="item_price" size="25" value="" >

<br><br>

Image Name: &nbsp; Include image extension - <strong>.jpg</strong><br>
<input type="text" name="image_name" size="45" value="" >

<br>
<br>

Category Name:<br>
<select name="cat_name">
<option value"" selected="selected">Select One</option>
<option value="Necklaces">Necklaces</option>
<option value="Bracelets">Bracelets</option>
<option value="Earrings"selected">Earrings</option>
<option value="Sets">Sets</option>
</select>

<br>
<br>

<p><input type="Submit" value="Add Item"></p>

</form>
and modify this insert script to put info into database:

Code: Select all

<?php
//connect to database
$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("databasename",$conn)  or die(mysql_error());

$query = "INSERT INTO items VALUES ('', '$item_name', '$item_desc', '$item_price', '$image_name', '$cat_name')";
mysql_query($query);

mysql_close();
?>
I need the image script to gather info on images - width, height, location - through hidden files and submit these into the database as well.

I tall order I know but I've got to put something together within 2 weeks.

Thanks
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Code: Select all

$currwidth = imagesx($simg);   // Current Image Width 
      $currheight = imagesy($simg);   // Current Image Height
Thats already in the script...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

What bit are you having difficulty with?

NOTE: That image thumbnail generation script is awful, it's doing a lot of unnecessary work, and it'll produce some really nasty looking thumbnails due to messing with the color palette and resizing rather than resampling the source image. If you downloaded that from somewhere I'd recommend continuing your search. Try starting here: http://www.phpgd.com/ ... there's some excellent thumbnail generation objects. I know they're excellent because I wrote them. :)
davidppppppppppp
Forum Newbie
Posts: 13
Joined: Mon Jul 03, 2006 9:31 pm

Post by davidppppppppppp »

If I start trying to cull one thing out and replacing it with another I'm going to end up with a script that doesn't work. I know that everyting in this script is tied together in one form or another. Taking something out will cause something else not to work.

I didn't think the thumbnails looked all that hot, but this script uploads the orginal image saving it to a folder and creates a thumbnail with the same (original) name and saves it. That's a feature I have to have and I've not found another script that will do that.

Other scripts will rename the image file - I can't be running all over the place trying to track down the new image file names.

So this will have to do. Now, I have to figure out how I can put all of this image information and the information from other form into one form and insert it into a database.

Thanks.

David - I'll have a look at your site.

You do know that the "full-size" size of the butterfly and the size of the butterfly image in your thumbnail is the same right?

I read this on another site and it's insane:
The other element that is included is a hidden form element called MAX_FILE_SIZE. The value of this element tells PHP the maximum size of file to allow. In practise it's more sensible to limit the uploaded file size using your php.ini file.
My web hosting company isn't going to turn over their php.ini file to me so I can start changing max image file sizes or anything else and neither is yours. So this is a moot point. Also MAX_FILE_SIZE is worthless, it doesn't work most of the time anyway.
Post Reply