2014-04-04 2 views
0

이미지에서 미리보기 이미지를 만들지 만 고정 크기 - 310px 너비, 217px 높이. 내 코드 : list($width, $height) = getimagesize($filename); $width = 310; $height = 217; $new_width = 310; $new_height = 217; $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);......미리보기 이미지, 고정 크기 만들기 - PHP

어떻게 만들 수 있습니까? 공식은 무엇입니까?

+0

가능한 중복 변경 PHP GD로 고정 너비/높이로 비례하여 자른 미리보기 이미지] (http://stackoverflow.com/questions/2894732/trying-to-generate-proportionally-cropped-thumbnails-at-a-fixed-width-height-wit) –

+0

나를위한 작품 : http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html –

답변

3
function resizeImage($url, $width, $height, $url_out, $keep_ratio = false){ 
    if($height <= 0 && $width <= 0) 
    return false; 
    else{ 

copy($url, $url_out); 
$info = getimagesize($url); 
$image = ''; 
$final_width = 0; 
$final_height = 0; 
list($width_old, $height_old) = $info; 
if($keep_ratio){ 
    if($width == 0) 
    $factor = $height/$height_old; 
    elseif($height == 0) 
    $factor = $width/$width_old; 
    else 
    $factor = min($width/$width_old, $height/$height_old); 
    $final_width = round($width_old * $factor); 
    $final_height = round($height_old * $factor); 
} 
else{ 
    $final_width = ($width <= 0) ? $width_old : $width; 
    $final_height = ($height <= 0) ? $height_old : $height; 
} 
switch($info[2]){ 
    case IMAGETYPE_GIF: 
    $image = imagecreatefromgif($url_out); 
    break; 
    case IMAGETYPE_JPEG: 
    $image = imagecreatefromjpeg($url_out); 
    break; 
    case IMAGETYPE_PNG: 
    $image = imagecreatefrompng($url_out); 
    break; 
    default: 
    return false; 
} 
$image_resized = imagecreatetruecolor($final_width, $final_height); 
if($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG){ 
    $transparency = imagecolortransparent($image); 
    if($transparency >= 0){ 
    $transparent_color = imagecolorsforindex($image, $trnprt_indx); 
    $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); 
    imagefill($image_resized, 0, 0, $transparency); 
    imagecolortransparent($image_resized, $transparency); 
    } 
    elseif($info[2] == IMAGETYPE_PNG){ 
    imagealphablending($image_resized, false); 
    $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); 
    imagefill($image_resized, 0, 0, $color); 
    imagesavealpha($image_resized, true); 
    } 
} 
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); 
switch($info[2]){ 
    case IMAGETYPE_GIF: 
    imagegif($image_resized, $url_out); 
    break; 
    case IMAGETYPE_JPEG: 
    imagejpeg($image_resized, $url_out); 
    break; 
    case IMAGETYPE_PNG: 
    imagepng($image_resized, $url_out); 
    break; 
    default: 
    return false; 
} 
imagedestroy($image_resized); 
return true; 
    } 
} 

// 단지 함수를 호출하고, [생성하려고의 이미지 이름을

$url="desert09.jpg"; 
resizeImage($url, 310, 217,"output.jpg", $keep_ratio = false); 
2

나는이 코드를 가지고있다. (나는 그것을 가지고있는 것을 기억하지 못한다.) 나는 그것을 약간 편집했다. 나는이 고정 된 사각형 크기가 엄지 손가락을 작성, 그것은 당신을 도울 수 있기를 바랍니다 :

/** Create a square cropped thumb **/ 
function createSquareCroppedThumb($path , $thumbPath, $thumbSize = 100){ 
     global $max_width, $max_height; 

    /* Set Filenames */ 
    $srcFile = $path; 
    $thumbFile = $thumbPath; 

    /* Determine the File Type */ 
    $type = pathinfo($path, PATHINFO_EXTENSION); 
    /* Create the Source Image */ 
    switch($type){ 
     case 'jpg' : case 'jpeg' : 
      $src = imagecreatefromjpeg($srcFile); break; 
     case 'png' : 
      $src = imagecreatefrompng($srcFile); break; 
     case 'gif' : 
      $src = imagecreatefromgif($srcFile); break; 
    } 
    /* Determine the Image Dimensions */ 
    $oldW = imagesx($src); 
    $oldH = imagesy($src); 

    $minValue = $oldH > $oldW ? $oldW : $oldH; 

    /* Create the New Image */ 
    $new = imagecreatetruecolor($thumbSize , $thumbSize); 

    /* Transcribe the Source Image into the New (Square) Image */ 
    imagecopyresampled($new , $src , 0 , 0 , ($oldW/2) - ($minValue /2) , ($oldH/2) - ($minValue /2) , $thumbSize , $thumbSize , $minValue , $minValue); 
    switch($type){ 
     case 'jpg' : case 'jpeg' : 
      $src = imagejpeg($new , $thumbFile); break; 
     case 'png' : 
      $src = imagepng($new , $thumbFile); break; 
     case 'gif' : 
      $src = imagegif($new , $thumbFile); break; 
    } 

    imagedestroy($new); 
} 
+0

이것은 310x310에서 잘 작동하지만 310x217에서 어떻게 작동합니까? –

+0

그것은 사각형에 대한 함수입니다 ... –