Jump to content

Recommended Posts

hello. I am trying to find a code that will resize an image to 600px wide, with the height scaling to it. I have been google this for awhile, but all I can find is info on how to display it as a different size. I need to be able to upload from a foreach loop, multiple images, with a smaller image....so that it saves as a smaller file size. so lets say the original is a 60" X 60"....I want my code to scale it down to a 10" X 10"(for example). I need it so that when uploaded, the file size is smaller as to allow the image to load faster. I don't think this makes much sense, but if it does, could you help me out with this?

Link to comment
https://forums.phpfreaks.com/topic/273461-resize-images/
Share on other sites

Ok, I was just never able to find anything that worked...I didn't realize I had the GD library until I found a code to test it out. any ways, I want this code from this website:

http://bgallz.org/502/php-upload-resize-image/

 

but I need to know how I can do a foreach for the images...it says can't redeclare...how would I do this as a foreach...or at least able to save more than one file. thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/273461-resize-images/#findComment-1407382
Share on other sites

btw, this is the code I have modified a bit:

<?php
// upload the file


if (isset($_POST["submit"])){





// index.php
function generate_resized_image(){
$max_dimension = 600; // Max new width or height, can not exceed this value.
$dir = "../images/"; // Directory to save resized image. (Include a trailing slash - /)
// Collect the post variables.
$postvars = array(
"image"    => trim($_FILES["image"]["name"]),
"image_tmp"    => $_FILES["image"]["tmp_name"],
"image_size"    => (int)$_FILES["image"]["size"],
"image_max_width"    => (int)$_POST["image_max_width"],
"image_max_height"   => (int)$_POST["image_max_height"]
);
// Array of valid extensions.
$valid_exts = array("jpg","jpeg","gif","png");
// Select the extension from the file.
$ext = end(explode(".",strtolower(trim($_FILES["image"]["name"]))));
// Check not larger than 175kb.
if($postvars["image_size"] <= 100000000000000000000){
// Check is valid extension.
if(in_array($ext,$valid_exts)){
if($ext == "jpg" || $ext == "jpeg"){
$image = imagecreatefromjpeg($postvars["image_tmp"]);
}
else if($ext == "gif"){
$image = imagecreatefromgif($postvars["image_tmp"]);
}
else if($ext == "png"){
$image = imagecreatefrompng($postvars["image_tmp"]);
}
// Grab the width and height of the image.
list($width,$height) = getimagesize($postvars["image_tmp"]);
// If the max width input is greater than max height we base the new image off of that, otherwise we
// use the max height input.
// We get the other dimension by multiplying the quotient of the new width or height divided by
// the old width or height.
if($postvars["image_max_width"] > $postvars["image_max_height"]){
if($postvars["image_max_width"] > $max_dimension){
$newwidth = $max_dimension;
} else {
$newwidth = $postvars["image_max_width"];
}
$newheight = ($newwidth / $width) * $height;
} else {
if($postvars["image_max_height"] > $max_dimension){
$newheight = $max_dimension;
} else {
$newheight = $postvars["image_max_height"];
}
$newwidth = ($newheight / $height) * $width;
}
// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight);
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
// Create random 4 digit number for filename.
$rand = rand(1000,9999);
$filename = $dir.$rand.$postvars["image"];
// Create image file with 100% quality.
imagejpeg($tmp,$filename,100);
return "<strong>Image Preview:</strong><br/>
<img src=\"".$filename."\" border=\"0\" title=\"Resized  Image Preview\" style=\"padding: 4px 0px 4px 0px;background-color:#e0e0e0\" /><br/>
Resized image successfully generated. <a href=\"".$filename."\" target=\"_blank\" name=\"Download your resized image now!\">Click here to download your image.</a>";
imagedestroy($image);
imagedestroy($tmp);
} else {
return "File size too large. Max allowed file size is .";
}
} else {
return "Invalid file type. You must upload an image file. (jpg, jpeg, gif, png).";
}
}











 mysql_query("INSERT INTO images (item_id, image)
VALUES ('$id123', 'images/".$remote_file1."')");



$title = $_POST['title'];

$des = mysql_real_escape_string($_POST['description']);
$price = mysql_real_escape_string($_POST['price']);
$qty = mysql_real_escape_string($_POST['quantity']);
$cat2 = mysql_real_escape_string($_POST['category2']);
$cat = mysql_real_escape_string($_POST['category']);


if ($cat2 !='')
{$cat = $cat2; }

mysql_query("INSERT INTO items (id, name, price, description, quantity, category_id, image)
VALUES ('$id123', '$title', '$price','$des', '$qty', '$cat', 'images/".$remote_file1."')");
if($cat2 !=''){
mysql_query("INSERT INTO categories (category)
VALUES ('$cat2')");
exit;
}
  }



?>

<form action="newprod.php?do=upload" method="post" enctype="multipart/form-data" name="form1">
<table width="0" border="0" cellspacing="2" cellpadding="2">
 <tr>
   <td width="69">

   <tr>
         <td>Name:</td>
         <td width="301"><div align="left">
           <input name="title" type="text" />
         </div></td>
   </tr>
       <tr>
         <td>Price:</td>
         <td><div align="left"><input name="price" type="text" /></div></td>
       </tr>
       <tr>
         <td>Quantity:</td>
         <td><div align="left"><input name="quantity" type="text" /></div></td>
       </tr>
       <tr>
         <td>Description:</td>
         <td><div align="left"><textarea name="description" cols="" rows=""></textarea></div></td>
       </tr>
        <tr>
         <td>Main Picture:</td>
         <td>
<input name="file1[]" type="file" /></td>
       </tr>
       <tr>
         <td>Picture:</td>
         <td>
<input name="image" type="file" multiple="multiple" /></td>
       </tr>
        <tr>
         <td>Category:</td>
         <td><div align="left">
           <label for="category"></label>

           <select name="category" id="category">
           <option value="">---Choose Category---</option>

           <?php 
$result = mysql_query("SELECT * FROM categories");

while($row = mysql_fetch_array($result))
 {
$category = $row['category'];
$id = $row['id'];

?>
<option value="<?php echo $category; ?>"><?php echo $category; ?></option>

<?php }
?>
           </select>
           <br />
           or make new category (leave blank if option is already selected):<br />
           <label for="category2"></label>
         <input type="text" name="category2" id="category2" />
         </td>
   </tr>
   <tr>
     <td> </td>
      <input name="image_max_width" type="hidden" id="image_max_width" value="600" size="4">


     <input name="image_max_height" type="hidden" id="image_max_height" value="1000000" size="4">
         </td>
   </tr>
       <tr>
         <td> </td>
     <td><div align="left"><input type="submit" name="submit" id="submit" value="Save"  /></div></td>
       </tr>
 </table>

</form>
<?php

if(isset($_GET['do']) && $_GET['do'] == "upload")
{

$upload_and_resize = generate_resized_image();
echo '<div id="upload">'. $upload_and_resize. '</div>';

}

?>

 

I know I will need to change image to image[](right?). can anyone help me out with this?

Link to comment
https://forums.phpfreaks.com/topic/273461-resize-images/#findComment-1407410
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.