PHP產生同比例的縮圖實現程式

來源:互聯網
上載者:User

 

建立映像縮圖需要許多時間,此代碼將有助於瞭解縮圖的邏輯。

 代碼如下 複製代碼

 
/**********************
*@filename - path to the image
*@tmpname - temporary path to thumbnail
*@xmax - max width
*@ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
    $ext = explode(".", $filename);
    $ext = $ext[count($ext)-1];

    if($ext == "jpg" || $ext == "jpeg")
        $im = imagecreatefromjpeg($tmpname);
    elseif($ext == "png")
        $im = imagecreatefrompng($tmpname);
    elseif($ext == "gif")
        $im = imagecreatefromgif($tmpname);

    $x = imagesx($im);
    $y = imagesy($im);

    if($x <= $xmax && $y <= $ymax)
        return $im;

    if($x >= $y) {
        $newx = $xmax;
        $newy = $newx * $y / $x;
    }
    else {
        $newy = $ymax;
        $newx = $x / $y * $newy;
    }

    $im2 = imagecreatetruecolor($newx, $newy);
    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
    return $im2;
}

例2

 代碼如下 複製代碼

function creat_thumbnail($img,$w,$h,$path)
{
 $org_info = getimagesize($img); //獲得映像大小且是通過post傳遞過來的
 //var_dump($org_info);
 //Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime] => image/png )
 $orig_x = $org_info[0]; //映像寬度
 $orig_y = $org_info[1]; //映像高度
 $orig_type = $org_info[2]; //圖片類別即尾碼 1 = GIF,2 = JPG,3 = PNG,

 //是真彩色映像
 if (function_exists("imagecreatetruecolor"))
 {
  switch($orig_type)
  {
   //從給定的gif檔案名稱中取得的映像
   case 1  : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif";
   break;
   //從給定的jpeg,jpg檔案名稱中取得的映像
   case 2  : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg";
   break;
   //從給定的png檔案名稱中取得的映像
   case 3  : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng";
   break;
  }
 }
 //如果從給定的檔案名稱可取得的映像
 if(function_exists($_function))
 {
  $orig_image = $_function($img); //從給定的$img檔案名稱中取得的映像
 }
 if (($orig_x / $orig_y) >= (4 / 3)) //如果寬/高 >= 4/3
 {
  $y = round($orig_y / ($orig_x / $w)); //對浮點數進行四捨五入
  $x = $w;
 }
 else //即 高/寬 >= 4/3
 {
  $x = round($orig_x / ($orig_y / $h));
  $y = $h;
 }
 $sm_image = imagecreatetruecolor($x, $y); //建立真彩色圖片
 //重採樣拷貝部分映像並調整大小
 Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y);
 //imageJPEG($sm_image, '', 80); //在瀏覽器輸出映像
 $tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //縮圖的路徑
 $thumbnail = @$_creatImage($sm_image, $tnpath, 80); //產生圖片,成功返回true(或1)
 imagedestroy ($sm_image); //銷毀映像
 if($thumbnail==true)
 {
  return $tnpath;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.