2011-01-27 5 views
-1

이미지를 내 사이트에 업로드하십시오. 이미지 너비는 498이고 높이는 402입니다. 최대 너비 250px 및 최대 높이 250px로 미리보기 이미지를 만들어야하지만 이미지는 250-250이어야하며 너비 250에 비례해야합니다. 픽셀.PHP 이미지의 비율

그것을 어떻게?

편집

나는 당신의 서버에 이미지를 업로드 할 수 있습니다. 너비 250 및 높이 250으로 만들고 싶은 크기의 한계. 이것은 이미지 1000h500을 업로드하면 250x250을 수행해야한다는 것을 의미하지 않습니다. 즉, 250 픽셀을하고 너비가 첫 번째 치수에 비례한다는 것을 의미하지는 않습니다. 결국, 저는 사진 250x125. 두 번째 예 : 이미지가 100h800입니다. 나는 그것을 바꿔야 함을 의미한다.

+3

이 매우 명확하지 않다. 필요한 것을 다시 말하거나 몇 가지 시각적 인 예나 다른 것을 주시겠습니까? – JakeParis

+0

@ JMC가 말한 것, 특히 비율을 어떻게 다뤄야 할까 두 번째. – Trufa

답변

-1
function makeThumbnail($image, $dest) 
    { 
     $imageType = exif_imagetype($image); 

     switch ($imageType) 
     { 
      case IMAGETYPE_JPEG: 
       $img = imagecreatefromjpeg($image); 
       break; 
      case IMAGETYPE_PNG: 
       $img = imagecreatefrompng($image); 
       break; 
      case IMAGETYPE_GIF: 
       $img = imagecreatefromgif($image); 
       break; 
      default: 
       throw new Im_Exception('Bad extension'); 
     } 

     $width = imagesx($img); 
     $height = imagesy($img);   

     if ($height > $width) 
     { 
      $ratio = 250/$height; 
      $newHeight = 250; 
      $newWidth = $width * $ratio; 
     } 
     else 
     { 
      $ratio = 250/$width; 
      $newWidth = 250; 
      $newHeight = $height * $ratio; 
     } 

     $previewImg = imagecreatetruecolor($newWidth, $newHeight); 

     $palsize = ImageColorsTotal($img); 
     for ($i = 0; $i < $palsize; $i++) 
     { 
      $colors = ImageColorsForIndex($img, $i); 
      ImageColorAllocate($previewImg, $colors['red'], $colors['green'], $colors['blue']); 
     } 

     imagecopyresized($previewImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 

     switch ($imageType) 
     { 
      case IMAGETYPE_JPEG: 
       $ext = 'jpg'; 
       imagejpeg($previewImg, $dest . '.' . $ext); 
       break; 
      case IMAGETYPE_PNG: 
      case IMAGETYPE_GIF: 
       $ext = 'png'; 
       imagesavealpha($previewImg, true); 
       imagepng($previewImg, $dest . '.' . $ext, 9); 
       break; 
      default: 
       throw new Im_Exception(); 
     } 
     imagedestroy($previewImg); 
     imagedestroy($img); 
    } 
+1

거의 동일하고 덜 모방 된 사본 ... –

1

나는이 목적을 위해서 ImageMagick 클래스를 사용할 것을 권장한다. 가 250x250 이미지를 만들어 저장하는 방법을 코드의 일부 문자열 :

$img = new Imagick('/path/to/image/image.jpg'); //image.jpg - your file 
$img->cropThumbnailImage(250, 250); //make thumbnail 250x250 
$img->writeImage('/newptah/newfilename.jpg'); //write thumbnail to new path 
$img->destroy(); //free resources 

newfilename.jpg는 - 비율을 잃지 않고 평방가 250x250 될 것이다.

+0

내 이미지의 크기가 1000x100 인 경우? 나는 무엇을 얻을 것인가? – Isis

+0

이 예제를 확인하십시오 : http://valokuva.org/?p=8 당신이 imagemagick의 새로운 버전을 가지고 있다면 그것은 의도 된 비율을 유지할 것입니다. – Michael

0

당신은 getimagesize 함수를 사용할 수 있습니다. 배열을 반환합니다.

$ size = getimagesize ($ filename);

$ width = $ size [0];

$ height => $ size [1]; 이미지의 폭이 높이보다 크기 때문에

그런 다음, 498분의 250

+2

그냥 fyi,이 방법은 청소기 : list ($ w, $ h) = getimagesize ($ filename);' – JakeParis

+0

물론. 나는 주된 문제에 집중하고 다른 모든 것을 단순하게 유지하려고 노력했다. – naiquevin

3

에 의해 원래의 폭과 높이를 곱 여기에 내가 GD를 사용하여 썸네일을 생성하기 위해 작성하는 기능입니다. 최대 너비 또는 높이 또는 둘 모두를 전달할 수 있으며 (0이면 무제한을 의미) 썸네일 비율은 그대로 비율이 $dest (+ 파일 확장자)로 조정됩니다. 또한 투명 이미지에서도 작동합니다. 여분의 공간은 완전히 투명해야합니다. 다른 배경을 원한다면 imagecopyresampled() 앞에있는 $img을 수정하십시오.

function picThumb($src, $dest, $width = 0, $height = 0, $quality = 100) 
    { 
    $srcType = exif_imagetype($src); 

    if (!$width && !$height) 
     { 
     $ext = image_type_to_extension($srcType, false); 
     copy($src, $dest . '.' . $ext); 
     return $ext; 
     } 

    ini_set('memory_limit', '134217728'); 
    try 
     { 
     switch ($srcType) 
      { 
      case IMAGETYPE_JPEG: 
       $srcImg = imagecreatefromjpeg($src); 
       break; 
      case IMAGETYPE_PNG: 
       $srcImg = imagecreatefrompng($src); 
       break; 
      case IMAGETYPE_GIF: 
       $srcImg = imagecreatefromgif($src); 
       break; 
      default: 
       throw new Exception(); 
      } 
     $srcWidth = imagesx($srcImg); 
     $srcHeight = imagesy($srcImg); 
     if (!$srcWidth || !$srcHeight) 
      { 
      throw new Exception(); 
      } 

     if ($width && $height) 
      { 
      $ratio = min($srcWidth/$width, $srcHeight/$height); 
      $areaWidth = round($width * $ratio); 
      $areaHeight = round($height * $ratio); 
      $areaX = round(($srcWidth - $areaWidth)/2); 
      $areaY = round(($srcHeight - $areaHeight)/2); 
      } 
     else // if (!$width || !$height) 
      { 
      if ($width) 
       { 
       $height = round($width/$srcWidth * $srcHeight); 
       } 
      else // if ($height) 
       { 
       $width = round($height/$srcHeight * $srcWidth); 
       } 
      $areaWidth = $srcWidth; 
      $areaHeight = $srcHeight; 
      $areaX = 0; 
      $areaY = 0; 
      } 

     $img = imagecreatetruecolor($width, $height); 
     imagealphablending($img, false); 
     imagecopyresampled($img, $srcImg, 0, 0, $areaX, $areaY, $width, $height, $areaWidth, $areaHeight); 

     switch ($srcType) 
      { 
      case IMAGETYPE_JPEG: 
       $ext = 'jpg'; 
       imagejpeg($img, $dest . '.' . $ext, $quality); 
       break; 
      case IMAGETYPE_PNG: 
      case IMAGETYPE_GIF: 
       $ext = 'png'; 
       imagesavealpha($img, true); 
       imagepng($img, $dest . '.' . $ext, 9); 
       break; 
      default: 
       throw new Exception(); 
      } 

     imagedestroy($srcImg); 
     imagedestroy($img); 
     } 
    catch (Exception $e) 
     { 
     ini_restore('memory_limit'); 
     throw $e; 
     } 
    ini_restore('memory_limit'); 

    return $ext; 
    }