ftp_put (resized image) coming out black

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kayo_potato
Forum Newbie
Posts: 7
Joined: Sat May 16, 2009 11:55 am

ftp_put (resized image) coming out black

Post by kayo_potato »

Hi,
I am struggling with a small piece of code, which I want to use to upload a thumbnail of an uploaded image.

I am managing to upload the original fine, but what I then want to do is :
1. resize the image using imagecopyresized and

2. save the resultant image in another folder on the server by ftp using ftp_put

This code is not working for me and I just have no idea what I am doing wrong, I have been able to find nothing to help me on the internet, hours of looking around.

Code: Select all

//this file comes from a form
$tmp_image=imagecreatefromjpeg($_FILES['uploadedfile']['tmp_name']); 
//make blank image of correct proportions
$new_image = imagecreatetruecolor($new_width,$new_height); 
//copy the uploaded image onto this new blank image, scaled.
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
//connect ftp
$conn_id = ftp_connect('$ftp');
ftp_login($conn_id,$username,$password);
 
//this is what I'd like to be able to do but it is just giving me a blank image.
//ftp_fput($conn_id,'wwwroot/naomi.jpeg', $fp, FTP_BINARY);
fclose($fp);
If anyone can help I would be extremely grateful.
Last edited by Benjamin on Sat May 16, 2009 1:06 pm, edited 1 time in total.
Reason: Changed code type from text to php.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: ftp_put (resized image) coming out black

Post by ldougherty »

Why are you moving it via FTP, is the other folder on the same server?

If its another folder on the same server just use the full path to the filename in the variable $tmp_image
kayo_potato
Forum Newbie
Posts: 7
Joined: Sat May 16, 2009 11:55 am

Re: ftp_put (resized image) coming out black

Post by kayo_potato »

Right, so instead of what I have, I would have
$tmp_image = path/to/previously/ftped/image;
Then what do I need to do with it? I am sorry if I seem useless, but this is really not my area, it is just a small part of a custom CMS I am making.
kayo_potato
Forum Newbie
Posts: 7
Joined: Sat May 16, 2009 11:55 am

Re: ftp_put (resized image) coming out black

Post by kayo_potato »

eh, sorry I meant
$temp_image = imagecreatefromjpeg(path/to/file/on/server);
kayo_potato
Forum Newbie
Posts: 7
Joined: Sat May 16, 2009 11:55 am

Re: ftp_put (resized image) coming out black

Post by kayo_potato »

Hi, I am still grappling with this, have altered my code a bit after reading around, but still not having any success.
I am confused about the imagejpeg's second parameter - is this an arbitrary path, or does it have to be a specific place or...?
If anyone can offer any insight I would be extremely grateful.
Thanks

Code: Select all

$conn_id = ftp_connect('ftp.mysite.com');
ftp_login($conn_id,$username,$password);
ftp_chdir($conn_id, 'wwwroot/');
$new_width = 100;
$new_height = 200;
 
$tmp_image=ImageCreateFromjpeg($_FILES['uploadedfile']['tmp_name']);
echo 'temp image is'.$tmp_image.'<br/>'; //fine - getting a resource number from this
$dest_image=ImageCreate(200, 100);
ImageCopyResized($dest_image, $tmp_image,0,0,0,0, $new_width, $new_height,762, 428);
$new_image=imagejpeg($dest_image, 'test.jpeg');//this fails
if(!$new_image){echo 'new image is not made<br/>';} //outputs 'new image is not made' because previous line fails
ftp_fput($conn_id,'test.jpeg', 'test.jpg', FTP_BINARY);
fclose($fp);
Last edited by Benjamin on Sun May 17, 2009 9:44 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: ftp_put (resized image) coming out black

Post by Benjamin »

Untested:

Code: Select all

 
<?php
class resize_image
{
    private $_quality = 85;
 
    public function __construct()
    {
    
    }
    
    public function process_resize($max_height, $max_width, $src_image, $dst_image, $allow_enlarge = false)
    {
        try {
            # make sure the src_image is there & readable
            if (!file_exists($src_image) || !is_readable($src_image))
            {
                throw new Exception("The image '$src_image' does not exist.");
            }
            
            # get the current height & width
            $cur_size = $this->get_image_size($src_image);
            
            if (!is_array($cur_size))
            {
                throw new Exception("Could not retrieve the height & width of '$src_image'.");
            }
            
            # pull the height and width out of the array
            list($cur_width, $cur_height) = $cur_size;
            
            # if the image is smaller than the maximum height & width we leave the values
            # alone unless $allow_enlarge is true
            if ($cur_width < $max_width && $cur_height < $max_height && !$allow_enlarge)
            {
                $new_width  = $cur_width;
                $new_height = $cur_height;
            } else {
                # calculate new dimensions
                list($new_width, $new_height) = $this->calculate_new_size($cur_height, $cur_width, $max_height, $max_width);
            }
            
            if (!$this->resample($cur_width, $cur_height, $new_width, $new_height, $src_image, $dst_image))
            {
                throw new Exception("An error occurred trying to resample the image.");
            }
 
            return array($new_width, $new_height);
        } catch (Exception $e) {
            trigger_error($e->getMessage(), E_USER_ERROR);
            return false;
        }
    }
    
    public function resample($cur_width, $cur_height, $new_width, $new_height, $src_image, $dst_image)
    {
        # create a blank true color image..
        $new_image = imagecreatetruecolor($new_width, $new_height);
 
        # create a new image based on the uploaded image..
        if (!$old_image = $this->new_image_by_type($src_image))
        {
            return false;
        }
        
        # resample and resize the image..
        if (!imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $cur_width, $cur_height))
        {
            return false;
        }
 
        # save image as a jpg and optimize the file size..
        return imagejpeg($new_image, $dst_image, 85);
    }
    
    public function new_image_by_type($src_image)
    {
        extract(pathinfo(strtolower($src_image)));
 
        switch($extension)
        {
            case "jpg":
                return imagecreatefromjpeg($src_image);
                break;
            case "jpeg":
                return imagecreatefromjpeg($src_image);
                break;
            case "gif":
                return imagecreatefromgif($src_image);
                break;
            case "png":
                return imagecreatefrompng($src_image);
                break;
            default:
                return false;
        }
    }
    
    public function calculate_new_size($cur_height, $cur_width, $max_height, $max_width)
    {
        if ($cur_width > $cur_height)
        {
            $new_width  = $max_width;
            $new_height = floor(($new_width * $cur_height) / $cur_width); 
        } else {
            $new_height = $max_height;
            $new_width  = floor(($new_height * $cur_width) / $cur_height);
        }
        
        return array($new_width, $new_height);
    }
    
    public function get_image_size($src_image)
    {
        if (!$size = getimagesize($src_image))
        {
            return false;
        }
        
        return $size;
    }
   
    
}
 
Post Reply