views:

52

answers:

4

Hi there,

I want to upload an image from disk, resize it, and then upload it to Amazon S3.

However, I cant get the proper image output from imagejpeg().

heres my code:

$sourceUrl = $_FILES['path']['tmp_name'];       
$thumbWidth = '100';
$thumbid = uniqid();
$img = imagecreatefromjpeg($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// output the image 
imagejpeg($tmp_img);

// upload thumbnail to s3
$s3->putObjectFile($tmp_img, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);

Firebug gives me this error :

illegal character
[Break on this error] (�����JFIF���������>CREATOR: g...(using IJG JPEG v62), default quality\n

If I modify imagejpeg this way,

imagejpeg($tmp_img, 'abc.jpg');

then I get the same error. :(

Can i get some help here please ?

A: 

You have to define the header:

header('Content-type: image/jpeg');
Radek Suski
It would be the right answer if he were trying to *output* the image, but he's trying to *store* it to Amazon.
Pekka
But also output it: imagejpeg($tmp_img); << imagejpeg — Output image to browser or file. If no filename has been specified then it will be displayed directly
Radek Suski
@Radek yes, but that's what he was doing wrong (among other things).
Pekka
Because of the comment "// output the image" I understood that he is trying to upload this file AND show it.
Radek Suski
+1  A: 

If you check the documentation of imagejpeg you can see it outputs the image, it means the way you call it it gets sent to the browser. You can get it to save to a file the second way you call it - by passing a filename in the second parameter.

Also, $tmp_img is an image resource, not a ready-to-use image file.

I don't know how your upload function works, but: if you need the file contents to upload, do it like this:

ob_start();
imagejpeg($tmp_image);
$image_contents = ob_get_clean();
$s3->putObjectFile($image_contents, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);

if you need a filename to upload:

$filename = tempnam(sys_get_temp_dir(), "foo");
imagejpeg($tmp_image, $filename);
$s3->putObjectFile($filename, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
Maerlyn
+1 This is the right answer.
Pekka
A: 

1) $tmp_img is a resource not a file. You probably need to save the image to disc and use that for putObjectFile

2) You probably need to tell S3 that the file you're uploading is of type image/jpeg

Pete
A: 

Well guys thank you very much again, I screwed around a bit more and combining that with your responses I got this working as follows :)

$thumbid .= ".jpg";
$img = imagecreatefromgif($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$path = '/var/www/1.4/wwwroot/cdn/'.$thumbid;
// output the image 
if(imagegif($tmp_img, $path)){                  
    $thumblink = "";
    // upload thumbnail to s3
    if($s3->putObjectFile($path, "mybucket", $thumbid, S3::ACL_PUBLIC_READ)){
        $thumblink = "http://dtzhqabcdscm.cloudfront.net/".$thumbid;
        imagedestroy($tmp_img);
    }
    return $thumblink;
}
Hrishikesh Choudhari