We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How can i automaticly resize an image after upload

Medium Priority
290 Views
Last Modified: 2013-12-13
Hello,
I'm desperate. I'm trying for almost two weeks now to change the following code in a way so it resizes an uploaded image to a maximum width of 350px.  It just won't work.
I hope someone can help me. I've included the initial code.
/*** Permission ***/
$bReadOnly0=false;
$bReadOnly1=false;
$bReadOnly2=false;
$bReadOnly3=false;
/*** /Permission ***/
 
$sBaseRoot0="";
$sBaseRoot1="";
$sBaseRoot2="";
$sBaseRoot3="";
$sBaseRoot0=ereg_replace($sBaseVirtual0,"",$sBase0); //output: "c:/inetpub/wwwroot"
if($sBase1!="")$sBaseRoot1=ereg_replace($sBaseVirtual1,"",$sBase1);
if($sBase2!="")$sBaseRoot2=ereg_replace($sBaseVirtual2,"",$sBase2);
if($sBase3!="")$sBaseRoot3=ereg_replace($sBaseVirtual3,"",$sBase3);
 
$sMsg = "";
$currFolder=$sBase0;
$ffilter="";
$sUploadedFile="";
$MaxFileSize = 1500000;
//$AllowedTypes = "gif|jpg";
$AllowedTypes = "*";
 
if(isset($_FILES["File1"]))
  {
  if(isset($_POST["inpCurrFolder2"]))$currFolder=$_POST['inpCurrFolder2'];
  if(isset($_REQUEST["inpFilter"]))$ffilter=$_REQUEST["inpFilter"];
 
  if($MaxFileSize && ($_FILES['File1']['size'] > $MaxFileSize))
    {
    $sMsg = "The file exceeds the maximum size allowed.";
    }
  else if(!isTypeAllowed($_FILES['File1']['name']))
    {
    $sMsg = "The File Type is not allowed.";
    }
  else if (move_uploaded_file($_FILES['File1']['tmp_name'], $currFolder."/".basename($_FILES['File1']['name'])))
    {
    $sMsg = "";
    $sUploadedFile=$_FILES['File1']['name'];
    @chmod($currFolder."/".basename($_FILES['File1']['name']), 0644);
    }
  else
    {
    $sMsg = "Upload failed.";
    }
  }
else
  {
  if(isset($_POST["inpCurrFolder"]))$currFolder=$_POST['inpCurrFolder'];
  if(isset($_REQUEST["ffilter"]))$ffilter=$_REQUEST["ffilter"];
  }
 
if(isset($_POST["inpFileToDelete"]))
  {
  $filename=pathinfo($_POST["inpFileToDelete"]);
  $filename=$filename['basename'];
  if($filename!="")
    unlink($currFolder . "/" . $filename);
  $sMsg = "";
  }

Open in new window

Comment
Watch Question

This should help.  Just scroll down to part II.  

http://www.4wordsystems.com/php_image_resize.php

Here's a routime that creates a thumbnail
a few mods and you can have it overwrite the original image

Reply if you need the mods or if this is enough to get you on the way


this code can go after your
@chmod($currFolder."/".basename($_FILES['File1']['name']), 0644);

$max_height=120;
$max_width=120;
    //
    // Let's create the thumbnail
    //
    $original_image=imagecreatefromjpeg('../item_images/'.$_FILES['userfile']['name']);
    $ratio=min($max_width/imagesx($original_image),$max_height/imagesy($original_image));
    $new_x=(int) ($ratio*imagesx($original_image)+.5);
    $new_y=(int) ($ratio*imagesy($original_image)+.5);
    $new_im = @ImageCreateTrueColor($new_x, $new_y) or die ("Cannot Initialize new GD image stream");
    $ok=imagecopyresampled( $new_im, $original_image, 0, 0, 0, 0, $new_x, $new_y, imagesx($original_image), imagesy($original_image));
    $ok=imagejpeg( $new_im, '../item_thumbs/'.$_FILES['userfile']['name'], 100);
    imagedestroy($original_image);
    imagedestroy($new_im);

Open in new window

Author

Commented:
Hi Michael,

Thnkx for the code however what is needed is the following. An original image has to be resized to a max width of 350px. That's the easy part. But the height has to change accordingly to the correct height so the image stays in proportion. (i hope that you understand what i mean. My english is a bit poor sometimes).
yes, in this routine you just have to set
$max_height=9999;
$max_width=350;

by setting high max height it will resize to width restriction
here's the code with the file name mods

note it's set to assume jpg uploads, if you have gif you'll have to change the imagecreatefromjpg and imagejpg lines
@chmod($currFolder."/".basename($_FILES['File1']['name']), 0644);
    $original_image=imagecreatefromjpeg($currFolder."/".basename($_FILES['File1']['name']));
    $ratio=min($max_width/imagesx($original_image),$max_height/imagesy($original_image));
    $new_x=(int) ($ratio*imagesx($original_image)+.5);
    $new_y=(int) ($ratio*imagesy($original_image)+.5);
    $new_im = @ImageCreateTrueColor($new_x, $new_y) or die ("Cannot Initialize new GD image stream");
    $ok=imagecopyresampled( $new_im, $original_image, 0, 0, 0, 0, $new_x, $new_y, imagesx($original_image), imagesy($original_image));
    $ok=imagejpeg( $new_im, $currFolder."/".basename($_FILES['File1']['name']), 100);
    imagedestroy($original_image);
    imagedestroy($new_im);

Open in new window

Author

Commented:
Ok, this works fine. The image is placed in the thumbs directory. What if i want to overwrite the orginal image in the maindirectory with the resized one and put a smaller image in the thumbs directory?
see above
$ok=imagejpeg( $new_im, $currFolder."/".basename($_FILES['File1']['name']), 100);

overwrites the original

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Michael,
Thanks a lot for your help. It works perfectly.

Kind regards,

Martin
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.