Image Resize and FTP Upload

Discussion in 'PHP' started by adamjblakey, Nov 10, 2008.

  1. #1
    Hi,

    I am having a problem with using the FTP function within php while resizing the image.

    I am using the following for uploading the image:

    
    function ftpupload($file, $img_tmp, $path){
    
       $message = '';
        if ($ftp = ftp_connect($_SESSION['ddomain'])) {
            if (ftp_login($ftp, $_SESSION['dname'], $_SESSION['dpass'])) {
                ftp_pasv($ftp, true);
                if (ftp_put($ftp, $path . $file,
                    $img_tmp, FTP_BINARY)) {
                    $message = "File uploaded";
                } else {
                    die("Could not upload file");
                }
            } else {
                die("Could not login to FTP account");
            }
        } else {
            die("Could not connect to FTP server");
        }
    	
    }
    
    PHP:
    Now this works fine when you pass it details for ['name'] and ['tmp_name'] but i want to use this with a image re-sizing/re-naming function which is:

    
    function uploadimage($value){
    	$file_type = $value['type'];
    	$file_name = $value['name'];
    	$file_size = $value['size'];
    	$file_tmp = $value['tmp_name'];
    
    //check file 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();
           }
    
    	//get the file extension.
    	$getExt = explode ('.', $file_name);
    	$file_ext = $getExt[count($getExt)-1];
    
    	//create a random file name
    	$rand_name = md5(time());
    	$rand_name= rand(0,999999999);
    	//get the new width variable.
    	$ThumbWidth = 175;
    	$ImgWidth = 700;
    
    	//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 width and height and keep height ratio.
    		list($width, $height) = getimagesize($file_tmp);
    		$imgratio=$width/$height;
    		if ($imgratio>1){
    			$newwidth = $ThumbWidth;
    			$newheight = $ThumbWidth/$imgratio;
    			$newwidth2 = $ImgWidth;
    			$newheight2 = $ImgWidth/$imgratio;
    			
    		}else{
    			$newheight = $ThumbWidth;
    			$newwidth = $ThumbWidth*$imgratio;
    			
    			$newheight2 = $ImgWidth;
    			$newwidth2 = $ImgWidth*$imgratio;
    		}
    		
    		
    		$resized_img = imagecreatetruecolor($newwidth, $newheight);
    		$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
    		
    		//save image
    		ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/thumbs/");
    		ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/big/");
    		
    		imagedestroy($resized_img);
    		imagedestroy($resized_img2);
    		imagedestroy($new_img);
    
    	}	
    	//$value = "$rand_name.$file_ext";
    	$value = $file_name;
    	return $value;
    } 
    
    PHP:
    At the moment this is all working fine and sends the image to the but just does not resize or rename. How can i get this to work how i want it to?

    Cheers,
    Adam
     
    adamjblakey, Nov 10, 2008 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    looks like you upload the big image into "thumb" and "big"
     
    happpy, Nov 10, 2008 IP
  3. mob4u1

    mob4u1 Well-Known Member

    Messages:
    951
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    110
    #3
    //save image
    ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/thumbs/");
    ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/big/");
    
    Code (markup):
    shouldn't it be this?:

    //save image
    ftpupload($[COLOR="Red"]resized_img[/COLOR], $file_tmp, "httpdocs/uploads/images/thumbs/");
    ftpupload($[COLOR="red"]resized_img2[/COLOR], $file_tmp, "httpdocs/uploads/images/big/");
    
    Code (markup):
    Daniel
     
    mob4u1, Nov 11, 2008 IP
  4. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're messing with the wrong parameter ;)

    The OP is indeed uploading the original image twice.. (2nd param)

    fix:

    
    ....
            $resized_img = imagecreatetruecolor($newwidth, $newheight);
            $resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
            imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
    
    $somePath = '/path/to/some/safe/temp/dir';
    imagejpeg ($resized_img, $somePath.'1.jpg');
    imagejpeg ($resized_img2, $somePath.'2.jpg');
           
            //save image
            ftpupload($file_name, $somePath.'1.jpg', "httpdocs/uploads/images/thumbs/");
            ftpupload($file_name, $somePath.'2.jpg', "httpdocs/uploads/images/big/");
           
            imagedestroy($resized_img);
            imagedestroy($resized_img2);
            imagedestroy($new_img);
    ....
    
    Code (markup):
    you could also use imagepng() instead of imagejpeg() but for non-vector pictures jpeg is simply smaller in filesize at the same quality.
     
    rene7705, Nov 12, 2008 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I tried this but it gives me the error of Could not upload file
     
    adamjblakey, Nov 17, 2008 IP
  6. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    This does not FTP upload the image as well though and that is what i need as it will be a remote server.
     
    adamjblakey, Nov 17, 2008 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Right i have got a step further. The image will now be uploaded with the name change but i still can't get the resized version uploaded. Can you see anything wrong?

    
    function uploadimage($value){
    	$file_type = $value['type'];
    	$file_name = $value['name'];
    	$file_size = $value['size'];
    	$file_tmp = $value['tmp_name'];
    
    //check file 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();
           }
    
    	//get the file extension.
    	$getExt = explode ('.', $file_name);
    	$file_ext = $getExt[count($getExt)-1];
    
    	//create a random file name
    	$rand_name = md5(time());
    	$rand_name= rand(0,999999999);
    	//get the new width variable.
    	$ThumbWidth = 175;
    	$ImgWidth = 700;
    
    	//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 width and height and keep height ratio.
    		list($width, $height) = getimagesize($file_tmp);
    		$imgratio=$width/$height;
    		if ($imgratio>1){
    			$newwidth = $ThumbWidth;
    			$newheight = $ThumbWidth/$imgratio;
    			$newwidth2 = $ImgWidth;
    			$newheight2 = $ImgWidth/$imgratio;
    			
    		}else{
    			$newheight = $ThumbWidth;
    			$newwidth = $ThumbWidth*$imgratio;
    			
    			$newheight2 = $ImgWidth;
    			$newwidth2 = $ImgWidth*$imgratio;
    		}
    		
    		
    		$resized_img = imagecreatetruecolor($newwidth, $newheight);
    		$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
    
    
    		imagejpeg ($resized_img, $rand_name.".".$file_ext);
    		imagejpeg ($resized_img2, $rand_name.".".$file_ext);
    
    		//save images
    		ftpupload($rand_name.".".$file_ext, $file_tmp, "httpdocs/uploads/images/thumbs/");
    		ftpupload($rand_name.".".$file_ext, $file_tmp, "httpdocs/uploads/images/big/");
    		
    		imagedestroy($resized_img);
    		imagedestroy($resized_img2);
    		imagedestroy($new_img);
    
    	}	
    	$value = $rand_name.".".$file_ext;
    	//$value = $file_name;
    	return $value;
    } 
    
    PHP:
     
    adamjblakey, Nov 17, 2008 IP