Jump to content

Resize Image


SkyRanger

Recommended Posts

Hey all,

 

Ok, here is what I need to do, I already have it to create a thumb, but I also need to condense the uploaded image down 50%.  Anybody have any idea, this is what I have so far:

 

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

	$path_thumbs = "thumbs/";
	$path_big = "images/";

	$img_thumb_width = 100; 

	$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
	$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

    $file_type = $_FILES['vImage']['type'];
            $file_name = $_FILES['vImage']['name'];
            $file_size = $_FILES['vImage']['size'];
            $file_tmp = $_FILES['vImage']['tmp_name'];
  
        if(!is_uploaded_file($file_tmp)){
           echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
           exit(); //exit the script and don't process the rest of it!
        }
       //check the file's extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);

       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit();
       }
       //so, whats the file's extension?
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       $rand_name = $file_name;

       //the new width variable
       $ThumbWidth = $img_thumb_width;

   //////////////////////////
   // CREATE THE THUMBNAIL //
   //////////////////////////
   
       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list the width and height and keep the height ratio.
           list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //finally, save the image
           ImageJpeg ($resized_img,"$path_thumbs/ies_thumb-$cid-$rand_name");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
        }
	move_uploaded_file ($file_tmp, "$path_big/ies-$cid-$rand_name");

 

I am probably looking at it right now and not even seeing it.

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

the problem I am running into is having the same image resize twice, not sure how to exactly do that

 

I was thinking of using imagecreatefrom but is there a simpler way of doing this instead of making one for each type of image.  ie jpg png gif etc... with the code I am currently using.

 

So what I am trying to figure out what do is:

 

upload image.ext

 

resize orig image.ext to 60%

then create thumbimage.ext to 100px x 100px all in the same code

 

 

Link to comment
https://forums.phpfreaks.com/topic/263929-resize-image/#findComment-1352570
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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