[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Change height/width of image in upload

Posted on 2007-03-26
3
Medium Priority
?
299 Views
Last Modified: 2008-02-01
I have this code that I use to upload an original image and then two smaller versions. It works fine.

CODE
-----------------
<?
if (!isset($_POST['action'])) {
?>
                  <form action="" method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="MAX_FILE_SIZE" value="2500000">
                        Select an image: <input type="file" name="imgfile" class="button"><br>
                        <br>
                        <center><font size="2">
                              <br>
                              For best results, use the dimensions (Height = 167px, Width = 200px).<br>
                              <br>
                              Please note: Only "jpg" images are currently supported. Files ending in ".jpeg" must be renamed to ".jpg" in order to be uploaded.<br>
                              <br>
                        </font>
                        <br>
                        <input type="hidden" name="action" value="do_upload">
                        <input type="submit" value="Submit Image">
                  </form><br><br>

<?

$result = mysql_query("SELECT * FROM bandinfo WHERE userid = '$ud_id'");
$num = mysql_num_rows($result);
$i = 0;
$pic = mysql_result($result,$i,"pic");

if (!empty($pic)){
?>
<?      
} else {
?>
<?
}
?>

<?
} elseif ($_POST['action'] == "do_upload") {
      $name = $_FILES['imgfile']['name'];
      if ($_FILES['imgfile']['type'] != 'image/jpeg' and $_FILES['imgfile']['type'] != 'image/pjpeg') {

?>

<?
exit;
      }
     
      $uploaddir1 = "pics";
      $uploaddir2 = "pics";
      $uploaddir3 = "pics";
     
     
      $final_filename1 = $ud_id."_l.jpg";
      $final_filename2 = $ud_id."_m.jpg";
      $final_filename3 = $ud_id."_s.jpg";
     
      $imgfile = $_FILES['imgfile']['tmp_name'];
     
      if (is_uploaded_file($imgfile)) {
            $newfile = $uploaddir1 . "/" . $final_filename1;
            copy($imgfile, $newfile);
           
            resize_me ($imgfile, $uploaddir2, $final_filename2, "75");
            resize_me ($imgfile, $uploaddir3, $final_filename3, "50");
           
            $query = "UPDATE `bandinfo` SET `pic` = '$ud_id' WHERE `userid` = '$ud_id'";
            mysql_query($query) or die('Error, insert query failed');
           
      }
     
?>

<?
}
           
function resize_me ($img, $path, $name, $percent) {
      //image file path
      //the folder to save to
      //the name to use when saving
      //percent to resize
     
      $orig_image = $img;
     
      $image_stats = GetImageSize($orig_image);
      $imagewidth = $image_stats[0];
      $imageheight = $image_stats[1];
      $img_type = $image_stats[2];
      $new_w = $imagewidth * $percent;
      $new_w = round($new_w / 100);
      $ratio = ($imagewidth / $new_w);
      $new_h = round($imageheight / $ratio);

      if ($img_type=="2") {
            $src_img = imagecreatefromjpeg($orig_image);
            $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));
            imagejpeg($dst_img, "$path"."/$name");
            } elseif  ($img_type=="3") {
            $dst_img=imagecreatetruecolor($new_w,$new_h);
            $src_img=ImageCreateFrompng($orig_image);
            imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
            Imagepng($dst_img, "$path"."/$name");
      }
}

?>

---------------


Is it possible to modify it so that I first resize the original image to Height = 167px, Width = 200px, save that file as the original and then create the smaller versions?
0
Comment
Question by:MDauphinais1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
babuno5 earned 2000 total points
ID: 18798221
I think the following code should work as per your requirement

    if (is_uploaded_file($imgfile)) {
            $newfile = $uploaddir1 . "/" . $final_filename1;
//            copy($imgfile, $newfile);
            resize_me ($imgfile, $uploaddir1, $final_filename1, '',167,200);
            resize_me ($newfile, $uploaddir2, $final_filename2, "75");
            resize_me ($newfile, $uploaddir3, $final_filename3, "50");
           
            $query = "UPDATE `bandinfo` SET `pic` = '$ud_id' WHERE `userid` = '$ud_id'";
            mysql_query($query) or die('Error, insert query failed');
           
      }


function resize_me ($img, $path, $name, $percent,$height="",$width="") {
      //image file path
      //the folder to save to
      //the name to use when saving
      //percent to resize
     
      $orig_image = $img;
     
      $image_stats = GetImageSize($orig_image);
      $imagewidth = $image_stats[0];
      $imageheight = $image_stats[1];
      $img_type = $image_stats[2];
            if($width == "")
            {
                  $new_w = $imagewidth * $percent;
                  $new_w = round($new_w / 100);
            }
            else
                  $new_w      = $width;
      
            if($height == "")
            {
            $ratio = ($imagewidth / $new_w);
              $new_h = round($imageheight / $ratio);
            }
            else
                  $new_h = $height;

      if ($img_type=="2") {
            $src_img = imagecreatefromjpeg($orig_image);
            $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));
            imagejpeg($dst_img, "$path"."/$name");
            } elseif  ($img_type=="3") {
            $dst_img=imagecreatetruecolor($new_w,$new_h);
            $src_img=ImageCreateFrompng($orig_image);
            imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
            Imagepng($dst_img, "$path"."/$name");
      }
}
0
 
LVL 6

Expert Comment

by:deresh
ID: 18798225
yes it is possible, but you will loose original aspect rato of image.

you just have to copy whole resize_me function to for example resize_me_fixed and replace $new_h with 176 and $new_w with 200. Then first call this new modified function and then call old one with 75 and 50 percent to make two smaller pictures.
0
 

Author Comment

by:MDauphinais1
ID: 18798629
That worked as posted. Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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