Jump to content

resize images


hbalagh

Recommended Posts

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";
}
} 


?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.