Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How can i automaticly resize an image after upload

Posted on 2009-02-09
8
Medium Priority
?
260 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

0
Comment
Question by:MartinSmits
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:digital0iced0
ID: 23594761
This should help.  Just scroll down to part II.  

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

0
 
LVL 19

Expert Comment

by:Michael701
ID: 23594772
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

0
 

Author Comment

by:MartinSmits
ID: 23594831
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).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Michael701
ID: 23594973
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
0
 
LVL 19

Expert Comment

by:Michael701
ID: 23595036
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

0
 

Author Comment

by:MartinSmits
ID: 23595083
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?
0
 
LVL 19

Accepted Solution

by:
Michael701 earned 2000 total points
ID: 23595109
see above
$ok=imagejpeg( $new_im, $currFolder."/".basename($_FILES['File1']['name']), 100);

overwrites the original
0
 

Author Closing Comment

by:MartinSmits
ID: 31544733
Michael,
Thanks a lot for your help. It works perfectly.

Kind regards,

Martin
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question