2013-04-13 5 views
0

저는 php에서 이미지 크기를 조정하는 데있어 매우 새로운 기능입니다. 기본적으로 업로드 된 이미지를 사용하여 축소판 이미지를 만들고 싶습니다. 내가 코드 아래 사용했지만 작동하지 않는 그것, 누군가가 나를 도울 수? .. 사전에 감사합니다 ...PHP에서 이미지 크기 조정

$source_image = $path.$row->photosfolder; 
$size = getimagesize($source_image); 
$w = $size[0]; 
$h = $size[1]; 
$simg = imagecreatefromjpeg($source_image); 
$dimg = imagecreatetruecolor(150, 225); 
$wm = $w/150; 
$hm = $h/225; 
$h_height = 225/2; 
$w_height = 150/2; 

if ($w > $h) { 
    $temp = imagecopyresampled($dimg, $simg, 0, 0, 0, 0, 150, 225, $w, $h); 
} 
elseif (($w < $h) || ($w == $h)) { 
    $temp = imagecopyresampled($dimg, $simg, 0, 0, 0, 0, 150, 225, $w, $h); 
} 
else { 
    $temp = imagecopyresampled($dimg, $simg, 0, 0, 0, 0, 150, 225, $w, $h); 
} 

$thumb_image = imagejpeg($dimg, $simg, 100); 
+0

그러면 문제가 생깁니 까? 당신은 그것이 작동하지 않는다고 말하지만 오류나 실제 문제가 무엇인지를 보여주지는 않습니다. – bestprogrammerintheworld

+0

Do not do $ temp = ... [php.net] (http://php.net/manual/en/function.imagecopyresampled.php)의 예를 따르십시오. – Daniel

+0

과도 할 수도 있지만 체크 아웃 할 수 있습니다. https : //github.com/avalanche123/Imagine – catchamonkey

답변

0

만약 교육 전체 코드를 찾을 수 있습니다 이미지 크기를 조정하려면 PHP 이미지 조작에 많은 메모리와 CPU 시간이 필요하고 서버 측에서 수행 할 필요가 없기 때문에 클라이언트 측에서 처리해야합니다 (db에 액세스 할 수없고 액세스 할 수 없습니다. 세션 등).

list($realW, $realH) = getimagesize($source_image); 

$realR = $realW/$realH; 
$thumbR = $thumbW/$thumbH; 

// If you want your resize image to fit inside the max thumb size : 

if($realR > $thumbR) // Real image if flatter than thumb 
{ 
    $newW = $thumbW; 
    $newH = $newW/$realR; 
} 
else 
{ 
    $newH = $thumbH; 
    $newW = $newH * $realR; 
} 

// Or if you want your resize image to be as small as possible but 
// not smaller than your thumb. This can be helpful in some cases. 

if($realR < $thumbR) 
{ 
    // Same code 
} 

그리고 당신이 얻을 수없는 경우 (PHP 매뉴얼을 읽었다로 사용 리샘플링 복사 : 당신은 여전히 ​​PHP에서 그것을하고 싶은 경우에, 당신은 기능이 올바른 크기를 얻을 수 있음을 사용할 수

작동하려면 함수의 개요 아래에 예제가 있습니다). 당신은 자바 스크립트를 사용하여 이미지 크기를 조정하려면

, 당신은 사용할 수 <canvas> :

같은
var canvas = document.createElement('canvas'); 
var image = document.getElementById('image'); 
var context = canvas.getContext('2d'); 
context.save(); 
context.drawImage(image, /* Here you put the right values using the algorithms above */); 
context.restore(); 
var thumb = document.createElement('image'); 
thumb.src = canvas.toDataUrl(); 

또는 뭔가. 특정 경우에 따라 몇 가지 사항을 변경할 수 있습니다.

0

아래 코드를 사용해보십시오. 이미지의 크기를 조정하고 새로운 미리보기 이미지를 만듭니다. 새 크기는 100 X 100으로 정의됩니다.이 예제는 또한 이미지의 종횡비를 유지합니다. 참고 :

1. 전체 경로를 설정하려면 이미지 경로가 디렉토리 경로가됩니다. 2. jpg 파일을 예로 들어 PGN & imagecreatefrompng, imagecreatefromgif가있는 GIF 파일을 사용할 수 있습니다. 3. PNG 파일을 만듭니다.

$_imagePath = 'somefile.jpg'; 
$im = imagecreatefromjpeg($_imagePath); 

imagealphablending($im, true); 

$_orgWidth = imagesx($im); 
$_orgHeight = imagesy($im); 

$_newWidth = 100; 
$_newHeight = 100; 

$_finalHeight = $_orgHeight * ($_newWidth/ $_orgWidth); 
    if($_finalHeight > $_newHeight){ 
     $_newWidth = $_orgWidth * ($_newHeight/$_orgHeight); 
    }else{ 
     $_newHeight = $_finalHeight ; 
    } 


$_thumb = imagecreatetruecolor($_newWidth, $_newHeight); 
imagealphablending($_thumb, true); 
imagesavealpha($_thumb, true); 

imagecopyresampled($_thumb, $im, 0, 0, 0, 0, $_newWidth, $_newHeight, $_orgWidth , $_orgHeight ); 

imagepng($_thumb, 'newname.png'); 
imagedestroy($_thumb);