php 圖片壓縮

來源:互聯網
上載者:User

標籤:

/**   * desription 判斷是否gif動畫   * @param sting $image_file圖片路徑   * @return boolean t 是 f 否   */  function check_gifcartoon($image_file){    $fp = fopen($image_file,‘rb‘);    $image_head = fread($fp,1024);    fclose($fp);    return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).‘NETSCAPE2.0‘."/",$image_head)?false:true;  }   /**  * desription 壓縮圖片  * @param sting $imgsrc 圖片路徑  * @param string $imgdst 壓縮後儲存路徑  */  function compressed_image($imgsrc,$imgdst){    list($width,$height,$type)=getimagesize($imgsrc);    $new_width = ($width>600?600:$width)*0.9;    $new_height =($height>600?600:$height)*0.9;    switch($type){      case 1:        $giftype=check_gifcartoon($imgsrc);        if($giftype){          header(‘Content-Type:image/gif‘);          $image_wp=imagecreatetruecolor($new_width, $new_height);          $image = imagecreatefromgif($imgsrc);          imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);          //75代表的是品質、壓縮圖片容量大小          imagejpeg($image_wp, $imgdst,75);          imagedestroy($image_wp);        }        break;      case 2:        header(‘Content-Type:image/jpeg‘);        $image_wp=imagecreatetruecolor($new_width, $new_height);        $image = imagecreatefromjpeg($imgsrc);        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);        //75代表的是品質、壓縮圖片容量大小        imagejpeg($image_wp, $imgdst,75);        imagedestroy($image_wp);        break;      case 3:        header(‘Content-Type:image/png‘);        $image_wp=imagecreatetruecolor($new_width, $new_height);        $image = imagecreatefrompng($imgsrc);        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);        //75代表的是品質、壓縮圖片容量大小        imagejpeg($image_wp, $imgdst,75);        imagedestroy($image_wp);        break;    }  }

 

php 圖片壓縮

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.