2016-08-25 7 views
0

이상한 코드 ... 내 기능은 다음과 같습니다PHP - ImageCreateFromJPEG() 반환 내가 서버에 의해 생성 된 다음의 웹 사이트의 사용자에 대한 HTML로 출력됩니다 축소판을 만들려고하고 있지만 이상한 코드를 반환

function createthumbnail($n){ 
    list($width, $height) = getimagesize($n); 
    $newheight = 100; 
    $ratio = 100/$height; 
    $newwidth = $width*$ratio; 
    $im = imagecreatetruecolor($newwidth, $newheight); 
    switch(exif_imagetype($n)){ 
    case "jpg": 
     $foto_r = imagecreatefromjpg($n); 
    break; 
    case "png": 
     $foto_r = imagecreatefrompng($n); 
    break; 
    case "bmp": 
     $foto_r = imagecreatefromwbmp($n); 
    break; 
    default: 
     $foto_r = imagecreatefromjpeg($n); 
    } 
    if(!$foto_r){ 
     $im = imagecreatetruecolor($newwidth, $newheight); 
     $bgc = imagecolorallocate($im, 255, 255, 255); 
     $tc = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 150, 30, $bgc); 
     imagestring($im, 1, 5, 5, 'Error loading ' . $n, $tc); 
    }else{ 
     imagecopyresampled($im, $foto_r, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
     imagejpeg($im, null, 100); 
    } 
    return $foto_r; 
} 
여기

는 이상한 코드의 예는 다음과 같습니다 Wierd code example

+2

그렇지 않습니다. 이상한 코드. 브라우저에 인쇄 내용을 알려주지 않았습니다. 'Content-Type' 헤더를 보내야합니다. 'imagejpeg' 앞에 넣으십시오, header ('Content-Type : image/jpeg'); –

+0

그러면 아무 것도 출력하지 않습니다. 웹 사이트의 왼쪽 상단에 그냥 그림 같은 프레임이 있습니다 ... –

+0

그건 당신의 코드가 만들어내는 그림입니다. 이제 함수가 올바른 미리보기 이미지를 만들지 않는 문제를 찾아보십시오. : P –

답변

1

내가 바로 다음 스크립트는 웹 페이지의 HTML과 하나 개의 실행에서 썸네일을 구축 보면? 아마 그런 거니?

<img src="<?php echo createthumbnail('path_to_the_image.jpeg'); ?>" alt="" /> 

썸네일은 이미지와 이미지 헤더를 생성하는 다른 PHP 스크립트와 같은 추가 파일이어야합니다.

<img src="generatethumbnail.php?f=path_to_the_image.jpeg" alt="" /> 

는 직접 HTML 코드에서 64 기수로 이미지 코드를 포함하는 "더러운"솔루션이 Embedding Base64 Images

예 :

<img src="data:image/jpeg;base64,<?php 
echo base64_encode(createthumbnail('path_to_the_image.jpeg')); 
?>" alt="" /> 

그것은 오래된 브라우저에서 지원되지는 모든 섬네일에 코드가로드되므로 이미지가 클라이언트에 캐시되지 않고 사이트 로딩 시간이 크게 늘어납니다.

+0

Worked! 대단히 감사합니다 !!! :삼 –