2010-05-14 5 views
3

최종 : 나는 기본적으로 이것을 사용하기로 결정했습니다 : http://shiftingpixel.com/2008/03/03/smart-image-resizer/이미지 크기 조정 : 불량 JPEG 품질과 검은 PNG 배경

이 모든 것을 처리하는 것처럼, 필자는 오프 캐싱 및 관리자 컨트롤러에서이 작업을 수행 설정 :

$image = file_get_contents(SITE_ADMIN_IMAGE.'/SmartImage.php?width='.$this->thumb_width.'&height='.$this->thumb_height.'&image=/images/'.$this->image_directory.'/'.$formData['image_url'].''); 
    file_put_contents(ROOT_PATH.'/public/images/'.$this->image_directory.'/thumb/'.$formData['image_url'], $image); 

편집 :이 작품을 발견하지만, 매우 날카로운 모서리를 만듭니다, 그것은 제대로 보이지 않습니다.

imagecolortransparent($dstImage, $background); 
    imagealphablending($dstImage, false); 
    $colorTransparent = imagecolorallocatealpha($dstImage, 0, 0, 0, 127); 
    imagefill($dstImage, 0, 0, $colorTransparent); 
    imagesavealpha($dstImage, true); 
    imagepng($dstImage, $toWhere); 

아이디어가 있습니까?

안녕하세요

,

내 클래스 두 가지 문제는 JPEG 이미지의 기본적 품질이 아주 가난하지만, 잘 모르겠어요 경우 내 비율 크기 조정에 이르기까지 이잖아. 이상적으로이 클래스는 이미지 크기와 수작업에 엄격 해지기를 바랍니다. 그러나 그 주위에 머리를 감을 수 없습니다.

내 주된 문제점은 png가 항상 검은 색 bg를 가지고 있다는 것인데, 누구도 이런 일을 경험 한 적이 있습니까? JPEG 이미지 품질에 관한

<?php 


    class OpenSource_ImageResize { 


     function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight) { 

      if ($mime == NULL) { 
       $mime = getimagesize($theFile); 
       $mime = $mime['mime']; 
      } 


      if ($mime == 'image/jpeg') { 
       $size = getimagesize($theFile); 

       if ($size[0] > $newWidth || $size[1] > $newHeight) { 
        $sourceImage = imagecreatefromjpeg($theFile); 
       } else { 
        return copy($theFile, $toWhere); 
        throw new exception('Could not create jpeg'); 
        return false; 
       } 
      } else if ($mime == 'image/png') { 
       $size = getimagesize($theFile); 

       if ($size[0] > $newWidth || $size[1] > $newHeight) { 
        $sourceImage = imagecreatefrompng($theFile); 
       } else { 
        return copy($theFile, $toWhere); 
        //throw new exception('Could not create png'); 
        return false; 
       } 
      } else if ($mime == 'image/gif') { 
       $size = getimagesize($theFile); 

       if ($size[0] > $newWidth || $size[1] > $newHeight) { 
        $sourceImage = imagecreatefromgif ($theFile); 
       } else { 
        return copy($theFile, $toWhere); 
        //throw new exception('Could not create gif'); 
        return false; 
       } 
      } else { 
       throw new exception('Not a valid mime type'); 
       return false; 
      } 

       $oldX = imageSX($sourceImage); 
       $oldY = imageSY($sourceImage); 

       if ($newWidth == NULL) { 
        $thumbHeight = $newHeight; 
        $thumbWidth = round($newHeight/($oldY/$oldX)); 
       } else 

       if ($oldX > $oldY) { 
        $thumbWidth = $newWidth; 
        $thumbHeight = $oldY * ($newHeight/$oldX); 
       } 

       if ($oldX < $oldY) { 
        $thumbWidth = round($newHeight/($oldY/$oldX)); 
        $thumbHeight = $newHeight; 
       } 

       if ($oldX == $oldY) { 
        $thumbWidth = $newWidth; 
        $thumbHeight = $newHeight; 
       } 

        if (!gd_info()) { 
         $dstImage = ImageCreate($thumbWidth, $thumbHeight); 
         imagecopyresized($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY); 
        } else { 
         $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight); 
         imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY); 
        } 

        if ($mime == 'image/png') { 
         $xparent = imagecolorresolvealpha($dstImage, 255,2,240, 0) ; 
         imagecolortransparent($dstImage,$xparent); 
         imagealphablending($dstImage,true); 
         imagepng($dstImage, $toWhere); 

        } else if ($mime == 'image/jpeg') { 
         imagejpeg($dstImage, $toWhere); 

        } else if ($mime == 'image/gif') { 
         imagegif ($dstImage, $toWhere); 

        } 

        imagedestroy($dstImage); 
        imagedestroy($sourceImage); 
        return true; 
     } 

} 

답변

2

당신이 imagejpeg()에서 세 번째 인수의 사용을해야합니다

imagejpeg($dstImage, $toWhere, 90); // any value above 85 should be fine 

PNG의 투명성에 관해서는, 당신은 잘못하고 있어요. 스크립트가 끔찍하고 근본적인 문제가 있습니다. 두 가지 문제를 모두 수정 한 개정판을 남겨 두겠습니다. 아직 더 최적화 할 수 있지만 분실 느끼지 않도록 나는 원래의 일부 덜 중요한 실수를 떠나 선택 :

class OpenSource_ImageResize 
{ 
    // $extension is not used? 
    function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight) 
    { 
     $sourceImage = ImageCreateFromString(file_get_contents($theFile)); 

     if (is_resource($sourceImage)) 
     { 
      $info = getimagesize($theFile); 

      if (is_null($mime)) 
      { 
       $mime = $info['mime']; 
      } 

      if ($info[0] <= $newWidth && $info[1] <= $newHeight) 
      { 
       imagedestroy($sourceImage); 
       return copy($theFile, $toWhere); 
      } 

      if (is_null($newWidth)) 
      { 
       $thumbHeight = $newHeight; 
       $thumbWidth = round($newHeight/($info[1]/$info[0])); 
      } 

      else if ($info[0] > $info[1]) 
      { 
       $thumbWidth = $newWidth; 
       $thumbHeight = $info[1] * ($newHeight/$info[0]); 
      } 

      if ($info[0] < $info[1]) 
      { 
       $thumbWidth = round($newHeight/($info[1]/$info[0])); 
       $thumbHeight = $newHeight; 
      } 

      if ($info[0] == $info[1]) 
      { 
       $thumbWidth = $newWidth; 
       $thumbHeight = $newHeight; 
      } 

      $dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight); 

      /* fix PNG transparency issues */   
      ImageFill($dstImage, 0, 0, IMG_COLOR_TRANSPARENT); 
      ImageSaveAlpha($dstImage, true); 
      ImageAlphaBlending($dstImage, true); 

      imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $info[0], $info[1]); 

      switch ($mime) 
      { 
       case 'image/png': 
        imagepng($dstImage, $toWhere, 9); // compress it (level 1 to 9) 
       break; 

       case 'image/jpeg': 
        imagejpeg($dstImage, $toWhere, 90); // quality = 90 (1 to 100, default is "about" 75) 
       break; 

       case 'image/gif': 
        imagegif($dstImage, $toWhere); 
       break; 
      } 

      imagedestroy($dstImage); 
      imagedestroy($sourceImage); 

      return true; 
     } 
    } 
} 

내가 명시 적으로 실수를 지적하지 않는 죄송하지만 이렇게 많은이며, 3시에 여기있어 잠을 자야 할 필요가 있습니다. 의심 스러우면 알려주세요.

+0

자고 깨어 응답 ..... –

+2

@HYDERALI : 뭐라 구요? –

-1

이 링크는 함수의 인수를 기반으로 크기를 조정하는 동안 자르기 - 맞기 또는 이미지 상자를 사용하는 간단한 함수로 연결됩니다. 그것은 또한 함수가 무엇을하는지에 관해 꽤 철저한 설명을 가지고있다.

http://www.spotlesswebdesign.com/blog.php?id=1

편집 : 고정 링크.

-1

Musty는 png 배경 색상을 정의합니다. 배경색을 정의한 후 jpg 및 기타 파일의 배경색을 변경할 수도 있습니다.