2010-06-23 4 views
2

디스크에서 이미지를 업로드하려면 크기를 조정하고을 다운로드 한 다음 Amazon S3에 업로드하십시오.imagejpeg()가 PHP에서 출력을 올바르게 제공하지 않습니다.

그러나 imagejpeg()에서 적절한 이미지 출력을 얻을 수 없습니다.

을 heres 내 코드 :

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

방화범이 나에게 오류를 제공합니다

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

내가 imagejpeg 이런 식으로 수정하면

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

은 저도 같은 오류가 발생합니다. :(

내가 여기에 약간의 도움을하시기 바랍니다받을 수

+0

당신이 설명하는 두 번째 방법은 작동해야 JPG 코드를 출력해서는 안됩니다. 100 % 확실하지 않니? 다른 곳에서 내용을 반향하고 있습니까? –

답변

3

당신이 출력 이미지를 볼 수 있습니다, 그것을 브라우저로 전송되는 방식을 의미하며 두 번째 매개 변수로 파일 이름을 전달하여 파일에 저장할 수 있습니다.

또한 10은 즉시 사용할 수있는 이미지 파일이 아니라 이미지 리소스입니다.

내가 업로드 기능이 어떻게 작동하는지 모르겠지만 :

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

업로드 할 파일 이름 필요한 경우 :이처럼 수행 업로드 할 파일 내용을 필요로하는 경우 :

$filename = tempnam(sys_get_temp_dir(), "foo"); 
imagejpeg($tmp_image, $filename); 
$s3->putObjectFile($filename, "mybucket", $thumbid, S3::ACL_PUBLIC_READ); 
+0

+1 이것은 올바른 대답입니다. –

0

당신은 헤더를 정의 할 필요가 : 당신이 imagejpeg의 문서를 확인하면

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

그가 이미지를 출력하려고한다면 올바른 대답이 될 것이지만 그는 Amazon에 저장하려고합니다. –

+0

또한 출력 : imagejpeg ($ tmp_img); << imagejpeg - 이미지를 브라우저 또는 파일로 출력합니다. 파일 이름이 지정되지 않은 경우 직접 –

+0

예로 표시되지만 그 중 무엇을 잘못했는지 (다른 것 중에서) 표시됩니다. –

0

1) $tmp_img은 파일이 아닙니다. 당신은 아마 디스크에 이미지를 저장하고) putObjectFile

2 당신은 아마 당신이 업로드중인 파일이

0

음의 사람은 매우 다시 한번 감사 형식 이미지/JPEG의 것을 S3 말할 필요가 사용할 필요가 , 조금 더 주위를 망쳐 놓은 귀하의 답변을 결합하여 나는 다음과 같이 일하고있어 :)

$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; 
} 
관련 문제