hbalagh Posted March 8, 2008 Share Posted March 8, 2008 I have this script but i would like it to not only make thumbnails (which is does already) but to resize the original image if it exceeds a width of lets say 500px this is the code from where i add the products <?php require_once("maxProtector.class.php"); include("../includes/db.conf.php"); include("../includes/connect.inc.php"); include ("../includes/functions.inc.php"); echo "<html><body bgcolor='#cccccc'><font face='verdana'><center>"; if (isset($_POST['submit'])){ if (file_exists($_FILES['image']['tmp_name'])){ $photoname= md5(rand()) . $_FILES['image']['name']; move_uploaded_file ($_FILES['image']['tmp_name'],"../images/".$photoname); thumbnail("../images/","../images/thumbs/",$photoname,100); //$rowa=mysql_fetch_array($mysqlresulta); }else{ $photoname="nophoto.jpg"; } $queryn="INSERT into products set name='$_POST[pname]', name2='$_POST[pname2]', photo='$photoname', description='$_POST[description]', category='$_POST[category]'"; $mysqlresultn = mysql_query($queryn); echo "New Product Added<br>"; echo mysql_error(); } echo "<h3>Add New Product</h3>"; echo "<form action='addproduct.php' method='post' enctype='multipart/form-data'> "; echo "<table>"; echo "<tr><td>Product ID</td>"; echo "<td><input name='pname' type='text' size='25' /></td></tr>"; echo "<tr><td colspan=2>Description</td></tr>"; echo "<tr><td colspan=2><textarea name='description' cols='30' rows='5'></textarea></td></tr>"; echo "<tr><td>Photo</td>"; echo "<td><input type='file' name='image'></td></tr>"; echo "<tr><td>Category</td>"; echo "<td><select name='category'>"; $query = "SELECT * from categories "; $mysqlresult = mysql_query($query); while($row = mysql_fetch_array($mysqlresult)){ echo "<option value='$row[id]'>$row[name]</option>"; } echo "</td></tr>"; echo "<tr><td colspan=2><input type='submit' name='submit' value='Submit'></td></tr>"; echo "</table>"; echo "</body></html>"; ?> here is the functions page code where i'm pretty sure that the code changes will take place but not sure. Any help on this would be greatly appreciaated... <?php function thumbnail($image_path,$thumb_path,$image_name,$thumb_width) { $system=explode(".",$image_name); if (!preg_match("/gif/",$system[1])){ if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg("$image_path/$image_name");} if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng("$image_path/$image_name");} //$src_img = imagecreatefromjpeg("$image_path/$image_name"); $origw=imagesx($src_img); $origh=imagesy($src_img); if ($origh>$origw){ $new_h=120; $new_w=120*($origw/$origh); }else{ $new_w=120; $new_h=120*($origh/$origw);} //$new_w = $thumb_width; //$diff=$origw/$new_w; //$new_h=$new_w; //$dst_img = imagecreate($new_w,$new_h); //imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); $dst_img = ImageCreateTrueColor($new_w,$new_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); if (preg_match("/png/",$system[1])){ imagepng($dst_img,"$thumb_path/$image_name"); } elseif (preg_match("/jpg|jpeg/",$system[1])){ imagejpeg($dst_img,"$thumb_path/$image_name"); }else{ imagegif($dst_img,"$thumb_path/$image_name"); } //imagejpeg($dst_img, "$thumb_path/$image_name"); return true; }else{ echo "You can only upload jpg and png files.<br>\n"; } } ?> Quote Link to comment Share on other sites More sharing options...
hbalagh Posted March 8, 2008 Author Share Posted March 8, 2008 can anyone help on this it would be so greatly appreciated Quote Link to comment Share on other sites More sharing options...
hbalagh Posted March 10, 2008 Author Share Posted March 10, 2008 can anyone offer any assistance at all please Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted March 10, 2008 Share Posted March 10, 2008 not 100% sure on how this can be done, but i did a quick google and found this site: http://www.sitepoint.com/article/image-resizing-php hope it helps. gl! Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 10, 2008 Share Posted March 10, 2008 If you're generating thumbnails, it should be very easy to resize the image... check for the size $size = getimagesize($filename); // $size[0] = image's width // $size[1] = image's height Check to see if the width is great than 500, if so, make a 'thumbnail' with 500 width if ($size[0] > 500) thumbnail($filename,$thumb_path,$image_name,500); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.