2012-04-09 4 views
0

입력란에 웹 사이트 URL을 제출하는 것으로부터 모든 이미지를 가져옵니다. 그것은 작동하지만 크기 이미지가 100X100보다 크거나 100x100보다 작은 경우에도이 이미지를 더 이상 늘리지 않아야합니다. 나는 streching하지 않고이 이미지가 100X100이되어야한다.이미지가 늘어납니다.

것은 내 이미지 "http://l.yimg.com/bt/api/res/1.2/vni2Ykd.HwvosAbFxUVzHQ--/YXBwaWQ9eW5ld3M7Y2g9MjAwOTtjcj0xO2N3PTMwODY7ZHg9MDtkeT0zOTA7Zmk9dWxjcm9wO2g9MjgwO3E9ODU7dz00MzA-/http://l.yimg.com/os/이 SRC를 가정 153/2012/04/08/142583283-10-jpg_170430.jpg "

이미지 크기를 100X100으로 지정하고 싶습니다. 이 이미지를 표시하는 동안 미리보기 이미지를 만드는 기능이 있습니다. 또는 당신이 가진 다른 해결책.

감사

+2

예. 이 사이트에서 "미리보기 이미지 만들기"를 검색하셨습니까? 그것은 잘 다루는 주제입니다. – deceze

답변

1

당신은 ... 이런 식으로 시도

이 resize.php

<?php 

    /** 

    * Image Resizer. 

    * @author : Harish Chauhan 

    * @copyright : Freeware 

    * About :This PHP script will resize the given image and can show on the fly or save as image file. 

    * 

    */ 





    define("HAR_AUTO_NAME",1); 

    Class RESIZEIMAGE 

    { 

     var $imgFile=""; 

     var $imgWidth=0; 

     var $imgHeight=0; 

     var $imgType=""; 

     var $imgAttr=""; 

     var $type=NULL; 

     var $_img=NULL; 

     var $_error=""; 



     /** 

     * Constructor 

     * 

     * @param [String $imgFile] Image File Name 

     * @return RESIZEIMAGE (Class Object) 

     */ 



     function RESIZEIMAGE($imgFile="") 

     { 
      if (!function_exists("imagecreate")) 

      { 

       $this->_error="Error: GD Library is not available."; 

       return false; 

      } 



      $this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM'); 

      if(!empty($imgFile)) 

       $this->setImage($imgFile); 

     } 

     /** 

     * Error occured while resizing the image. 

     * 

     * @return String 

     */ 

     function error() 

     { 

      return $this->_error; 

     } 



     /** 

     * Set image file name 

     * 

     * @param String $imgFile 

     * @return void 

     */ 

     function setImage($imgFile) 

     { 

      $this->imgFile=$imgFile; 

      return $this->_createImage(); 

     } 

     /** 

     * 

     * @return void 

     */ 

     function close() 

     { 

      return @imagedestroy($this->_img); 

     } 

     /** 

     * Resize a image to given width and height and keep it's current width and height ratio 

     * 

     * @param Number $imgwidth 

     * @param Numnber $imgheight 

     * @param String $newfile 

     */ 

     function resize_limitwh($imgwidth,$imgheight,$newfile=NULL) 

     { 

      $image_per = 100; 

      list($width, $height, $type, $attr) = @getimagesize($this->imgFile); 

      if($width > $imgwidth && $imgwidth > 0) 

       $image_per = (double)(($imgwidth * 100)/$width); 



      if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0) 

       $image_per = (double)(($imgheight * 100)/$height); 



      $this->resize_percentage($image_per,$newfile); 



     } 

     /** 

     * Resize an image to given percentage. 

     * 

     * @param Number $percent 

     * @param String $newfile 

     * @return Boolean 

     */ 

     function resize_percentage($percent=100,$newfile=NULL) 

     { 

      $newWidth=($this->imgWidth*$percent)/100; 

      $newHeight=($this->imgHeight*$percent)/100; 

      return $this->resize($newWidth,$newHeight,$newfile); 

     } 

     /** 

     * Resize an image to given X and Y percentage. 

     * 

     * @param Number $xpercent 

     * @param Number $ypercent 

     * @param String $newfile 

     * @return Boolean 

     */ 

     function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL) 

     { 

      $newWidth=($this->imgWidth*$xpercent)/100; 

      $newHeight=($this->imgHeight*$ypercent)/100; 

      return $this->resize($newWidth,$newHeight,$newfile); 

     } 



     /** 

     * Resize an image to given width and height 

     * 

     * @param Number $width 

     * @param Number $height 

     * @param String $newfile 

     * @return Boolean 

     */ 

     function resize($width,$height,$newfile=NULL) 

     { 

      if(empty($this->imgFile)) 

      { 

       $this->_error="File name is not initialised."; 

       return false; 

      } 

      if($this->imgWidth<=0 || $this->imgHeight<=0) 

      { 

       $this->_error="Could not resize given image"; 

       return false; 

      } 

      if($width<=0) 

       $width=$this->imgWidth; 

      if($height<=0) 

       $height=$this->imgHeight; 



      return $this->_resize($width,$height,$newfile); 

     } 



     /** 

     * Get the image attributes 

     * @access Private 

     *  

     */ 

     function _getImageInfo() 

     { 

      @list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)[email protected]($this->imgFile); 

      $this->imgType=$this->type[$type]; 

     } 



     /** 

     * Create the image resource 

     * @access Private 

     * @return Boolean 

     */ 

     function _createImage() 

     { 

      $this->_getImageInfo($this->imgFile); 

      if($this->imgType=='GIF') 

      { 

       $this->[email protected]($this->imgFile); 

      } 

      elseif($this->imgType=='JPG') 

      { 

       $this->[email protected]($this->imgFile); 

      } 

      elseif($this->imgType=='PNG') 

      { 

       $this->[email protected]($this->imgFile); 

      }   

      if(!$this->_img || [email protected]_resource($this->_img)) 

      { 

       $this->_error="Error loading ".$this->imgFile; 

       return false; 

      } 

      return true; 

     } 



     /** 

     * Function is used to resize the image 

     * 

     * @access Private 

     * @param Number $width 

     * @param Number $height 

     * @param String $newfile 

     * @return Boolean 

     */ 

     function _resize($width,$height,$newfile=NULL) 

     { 

      if (!function_exists("imagecreate")) 

      { 

       $this->_error="Error: GD Library is not available."; 

       return false; 

      } 



      [email protected]($width,$height); 

      //imagecolortransparent($newimg, imagecolorat($newimg, 0, 0)); 



      if($this->imgType=='GIF' || $this->imgType=='PNG') 

      { 

       /** Code to keep transparency of image **/ 

       $colorcount = imagecolorstotal($this->_img); 

       if ($colorcount == 0) $colorcount = 256; 

       imagetruecolortopalette($newimg,true,$colorcount); 

       imagepalettecopy($newimg,$this->_img); 

       $transparentcolor = imagecolortransparent($this->_img); 

       imagefill($newimg,0,0,$transparentcolor); 

       imagecolortransparent($newimg,$transparentcolor); 

      } 



      @imagecopyresampled ($newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight); 







      if($newfile===HAR_AUTO_NAME) 

      { 

       if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)) 

        [email protected]_replace($this->imgFile,"_har",[email protected]($matches[0]),0);    

      } 

      elseif(!empty($newfile)) 

      { 

       if([email protected]_match("/\..*+$/",@basename($newfile))) 

       { 

        if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)) 

         $newfile=$newfile.$matches[0]; 

       } 

      } 



      if($this->imgType=='GIF') 

      { 

       if(!empty($newfile)) 

        @imagegif($newimg,$newfile); 

       else 

       { 

        @header("Content-type: image/gif"); 

        @imagegif($newimg); 

       } 

      } 

      elseif($this->imgType=='JPG') 

      { 

       if(!empty($newfile)) 

        @imagejpeg($newimg,$newfile); 

       else 

       { 

        @header("Content-type: image/jpeg"); 

        @imagejpeg($newimg); 

       } 

      } 

      elseif($this->imgType=='PNG') 

      { 

       if(!empty($newfile)) 

        @imagepng($newimg,$newfile); 

       else 

       { 

        @header("Content-type: image/png"); 

        @imagepng($newimg); 

       } 

      } 

      @imagedestroy($newimg); 

     } 

    } 

?> 

이 코드 사용에 파일 resize.php을 포함 resize.php에 아래의 코드를 저장 할 수 있습니다 아래 코드는 이미지의 크기를 조정하거나 축소판을 만듭니다.

include "resize.php"; 
$path = "your image path"; 
$newname = "your image name to be resized"; 

$rimg=new RESIZEIMAGE("$path/$newname"); 
$width = $rimg->imgWidth; 
if($width > 800){ 
    $per = round(800/$width*100); 
    $rimg->resize_percentage($per,"$path/$newname"); //resize image 
} 
$rimg->resize(100,100,"$path/$newname"); //create thumbnail for an image 
$rimg->close(); 
관련 문제