PHPͼƬ²Ã¼ôÓëËõ·ÅʾÀý£¨ÎÞËð²Ã¼ôͼƬ£©

5年以前  |  阅读数:613 次  |  编程语言:PHP 

±¾ÎĽeÉÜÁËPHPͼƬ²Ã¼oÓeËoÅʾÀý£¬Ï»°²»¶aÉÙ£¬¾ßÌa´uÂeÈçÏ£º


    /*

     *exif_imagetype -- ÅжÏÒ»¸oͼÏñµÄÀaÐÍ

     *¹¦ÄÜ˵Ã÷£ºº¯Êý¹¦ÄÜÊÇ°ÑÒ»¸oͼÏñ²Ã¼oΪÈÎÒa´oСµÄͼÏñ£¬²¢±£³ÖͼÏñ²»±aÐÎ

     *²ÎÊý˵Ã÷£ºÊaÈe ÐeÒª´¦ÀiͼƬµÄ ÎļþÃu£¬Éu³ÉÐÂͼƬµÄ±£´aeÎļþÃu£¬Éu³ÉÐÂͼƬµÄ¿i£¬Éu³ÉÐÂͼƬµÄ¸ß

     */



    // »ñµÃÈÎÒa´oСͼÏñ£¬²»×aµØ*½À­Éi£¬²»²uÉu±aÐΣ¬²»ÁoÏ¿հ×

    function image_resize($src_file, $dst_file, $new_width, $new_height)

    {

      $new_width = intval($new_width);



      $new_height = intval($new_width);



      if ($new_width < 1 || $new_height < 1)

      {

        echo "params width or height error !";

        exit();

      }



      if (!file_exists($src_file))

      {

        echo $src_file . " is not exists !";

        exit();

      }



      // ͼÏñÀaÐÍ

      $type = exif_imagetype($src_file);



      $support_type = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);



      if (!in_array($type, $support_type, true))

      {



        echo "this type of image does not support! only support jpg , gif or png";

        exit();

      }



      //Load image



      switch($type)

      {

        case IMAGETYPE_JPEG :



          $src_img = imagecreatefromjpeg($src_file);



          break;



        case IMAGETYPE_PNG :



          $src_img = imagecreatefrompng($src_file);



          break;



        case IMAGETYPE_GIF :



          $src_img = imagecreatefromgif($src_file);



          break;



        default :

          echo "Load image error!";



          exit();

      }



      $w = imagesx($src_img);



      $h = imagesy($src_img);



      $ratio_w = 1.0 * $new_width / $w;



      $ratio_h = 1.0 * $new_height / $h;



      $ratio = 1.0;



      // Éu³ÉµÄͼÏñµÄ¸ß¿i±ÈÔ­À´µÄ¶¼Ð¡£¬»o¶¼´o £¬Ô­ÔoÊÇ È¡´o±ÈÀý*Å´o£¬È¡´o±ÈÀýËoС£¨ËoСµÄ±ÈÀý¾Í±È½ÏСÁË£(C)



      if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1))

      {



        if ($ratio_w < $ratio_h) {



          $ratio = $ratio_h;



          // Çe¿oÒ»£¬¿i¶ÈµÄ±ÈÀý±È¸ß¶È*½ÏoµÄС£¬°´Õո߶ȵıÈÀý±e×¼À´²Ã¼o»o*Å´o



        } else {



          $ratio = $ratio_w;



        }



        // ¶¨ÒaÒ»¸oÖмaµÄÁÙʱͼÏñ£¬¸ÃͼÏñµÄ¿i¸ß±È ÕýºÃÂu×aÄ¿±eÒªÇo



        $inter_w = (int)($new_width / $ratio);



        $inter_h = (int)($new_height / $ratio);



        $inter_img = imagecreatetruecolor($inter_w, $inter_h);



        //var_dump($inter_img);



        imagecopy($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h);



        // Éu³ÉÒ»¸oÒÔ×i´o±ß³¤¶ÈΪ´oСµÄÊÇÄ¿±eͼÏñ$ratio±ÈÀýµÄÁÙʱͼÏñ

        // ¶¨ÒaÒ»¸oеÄͼÏñ



        $new_img = imagecreatetruecolor($new_width, $new_height);



        //var_dump($new_img);exit();

        imagecopyresampled($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h);



        switch($type)

        {

          case IMAGETYPE_JPEG :



            // ´ae´¢Í¼Ïñ

            imagejpeg($new_img, $dst_file, 100);



            break;



          case IMAGETYPE_PNG :



            imagepng($new_img, $dst_file, 100);



            break;



          case IMAGETYPE_GIF :



            imagegif($new_img, $dst_file, 100);



            break;



          default :



            break;

        }



      }// end if 1



      // 2 Ä¿±eͼÏñ µÄÒ»¸o±ß´oÓÚԭͼ£¬Ò»¸o±ßСÓÚԭͼ £¬ÏÈ*Å´oƽÆÕͼÏñ£¬È»ºo²Ã¼o



      // =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )



      else {



        $ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w;



        //È¡±ÈÀý´oµÄÄǸoÖµ



        // ¶¨ÒaÒ»¸oÖмaµÄ´oͼÏñ£¬¸ÃͼÏñµÄ¸ß»o¿iºÍÄ¿±eͼÏñÏaµÈ£¬È»ºo¶Ôԭͼ*Å´o



        $inter_w = (int)($w * $ratio);



        $inter_h = (int)($h * $ratio);



        $inter_img = imagecreatetruecolor($inter_w, $inter_h);



        //½«Ô­Í¼Ëo*űÈÀýºo²Ã¼o



        imagecopyresampled($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h);



        // ¶¨ÒaÒ»¸oеÄͼÏñ



        $new_img = imagecreatetruecolor($new_width, $new_height);



        imagecopy($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height);



        switch($type)

        {

          case IMAGETYPE_JPEG :



            // ´ae´¢Í¼Ïñ

            imagejpeg($new_img, $dst_file, 100);



            break;



          case IMAGETYPE_PNG :



            imagepng($new_img, $dst_file, 100);

            break;



          case IMAGETYPE_GIF :



            imagegif($new_img, $dst_file, 100);



            break;



          default :



            break;

        }



      }// if3



    }// end function

    //Êa³oÐÂͼƬ

    image_resize('test.jpg', 'demo.jpg', '1200px', '1200px');

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例