2014-06-07 2 views
0

썸네일을 만들고 있는데 어떤 이유로 출력물이 올바른 크기이지만 항상 검은 색입니다. 유사한 주제에 대해 또 다른 Stack Overflow 게시물을 보았습니다. 그러나 그의 경우에는 매개 변수를 잘못 전달했습니다.왜 크기 조정 된 이미지가 항상 검은 색입니까?

나는 비디오 카메라에서 이미지를 캡처하고 다음이 코드를 사용하여 :

내가 얻을 결과는 적절한 디렉토리에 저장 두 파일 모두 적절한 크기이지만, 썸네일은 모두 검은 색이다
$data = base64_decode($data); // the data will be saved to the db 

$image = imagecreatefromstring($data); // need to create an image to grab the width and height 
$img_width = imagesx($image); 
$img_height = imagesy($image); 

// calculate thumbnail size 
$new_height = 100; 
$new_width = floor($img_width * (100/$img_height)); 

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

// copy and resize old image into new image 
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
$url = IMGDIR.$imgname; 
$thumburl = IMGDIR."thumb/".$imgname; 

// save image and thumb to disk 
imagepng($image,$url); 
imagepng($new_image,$thumburl); 

. 내가 누락 된 단순한 것이 있어야합니다. 어떤 아이디어?

답변

1

PNG 파일은 알파 채널을 기억하기위한 true을 전달하십시오. 따라서 imagealphablendingimagesavealpha을 사용해야합니다. 여기에 코드에 통합됩니다.

$data = base64_decode($data); // the data will be saved to the db 

$image = imagecreatefromstring($data); // need to create an image to grab the width and height 
$img_width = imagesx($image); 
$img_height = imagesy($image); 

// calculate thumbnail size 
$new_height = 100; 
$new_width = floor($img_width * (100/$img_height)); 

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

// copy and resize old image into new image 
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height); 
$url = IMGDIR . $imgname; 
$thumburl = IMGDIR . "thumb/" . $imgname; 

// Set the image alpha blending settings. 
imagealphablending($image, false); 
imagealphablending($new_image, false); 

// Set the image save alpha settings. 
imagesavealpha($image, true); 
imagesavealpha($new_image, true); 

// save image and thumb to disk 
imagepng($image,$url); 
imagepng($new_image,$thumburl); 
+0

시도했습니다. 큰 이미지는 저장되며 미리보기 이미지는 여전히 검정색입니다. 나는 jpg도 시도했다. –

+0

제가 이미지 소스와 관련이 있는지 궁금합니다. 그것은 비디오 캡처에서 base64 스트림입니다. 먼저 해독하고 저장하기 위해 작동하지만 스케일링에 필요한 정보가 누락 되었습니까? –

+0

@DougWolfgram 미리보기 이미지를 아주 쉽게 수정합니다. 지금 내 코드를 사용해보십시오. 게시 된 코드에서 '$ img_width' 및'$ img_height'를 사용하여 원본 이미지 높이 및 너비를 할당합니다. 그러나 원래의'imagecopyresampled' 라인을보십시오. 정의되지 않은 변수 인'$ width'와'$ height'를 사용하고 있습니다. 그래서 나는 그 정답으로 내 대답을 편집 했으므로 지금 사업에 있어야합니다. – JakeGould

1

imagesavealpha와 이미지의 알파 채널을 저장하고 두번째 인수

imagesavealpha($image, true); 
imagepng($image,$url); 

imagesavealpha($new_image, true); 
imagepng($new_image,$thumburl); 
관련 문제