开发者

imagejpeg() doesnt give the output properly in php

开发者 https://www.devze.com 2023-01-04 22:47 出处:网络
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().

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开发者_JS百科_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 ?


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);


You have to define the header:

header('Content-type: image/jpeg');


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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消