首頁  >  文章  >  後端開發  >  php 縮放圖片實例簡介

php 縮放圖片實例簡介

怪我咯
怪我咯原創
2017-07-10 11:57:341520瀏覽

設定寬高,不等比例縮放;設定寬度,等比例縮放;設定高度,等比例縮放;按比例,縮放至50%;縮放後直接輸出到螢幕等等,具體使用方法如下,有興趣的額朋友可以了解下哈


使用方法:

設定寬高,不等比例縮放

# 程式碼如下:

<?php 
include(&#39;SimpleImage.php&#39;); 
$image = new SimpleImage(); 
$image->load(&#39;picture.jpg&#39;); 
$image->resize(250,400); 
$image->save(&#39;picture2.jpg&#39;);?> 

设定宽度,等比例缩放

<?php 
include(&#39;SimpleImage.php&#39;); 
$image = new SimpleImage(); 
$image->load(&#39;picture.jpg&#39;); 
$image->resizeToWidth(250); 
$image->save(&#39;picture2.jpg&#39;);?> 

设定高度,等比例缩放

<?php 
include(&#39;SimpleImage.php&#39;); 
$image = new SimpleImage(); 
$image->load(&#39;picture.jpg&#39;); 
$image->resizeToHeight(500); 
$image->save(&#39;picture2.jpg&#39;); 
$image->resizeToHeight(200); 
$image->save(&#39;picture3.jpg&#39;);?> 

按比例,缩放至50%

<?php 
include(&#39;SimpleImage.php&#39;); 
$image = new SimpleImage(); 
$image->load(&#39;picture.jpg&#39;); 
$image->scale(50); 
$image->save(&#39;picture2.jpg&#39;);?>

缩放后直接输出到屏幕

<?php 
header(&#39;Content-Type: image/jpeg&#39;); 
include(&#39;SimpleImage.php&#39;); 
$image = new SimpleImage(); 
$image->load(&#39;picture.jpg&#39;); 
$image->resizeToWidth(150); 
$image->output();?>

有時上傳圖片時因為圖片太大了,不僅佔用空間,消耗流量,而且影響 參考(圖片的尺寸大小不一)。下面分享一種等比例不失真縮放圖片的方法,這樣,不管上傳的圖片尺有多大,都會自動壓縮到我們設定尺寸值的範圍之內。經過測試,證明實用。

<?php
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
 {
 $pic_width = imagesx($im);
 $pic_height = imagesy($im);
 if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
 {
  if($maxwidth && $pic_width>$maxwidth)
  {
  $widthratio = $maxwidth/$pic_width;
  $resizewidth_tag = true;
  }
  if($maxheight && $pic_height>$maxheight)
  {
  $heightratio = $maxheight/$pic_height;
  $resizeheight_tag = true;
  }
  if($resizewidth_tag && $resizeheight_tag)
  {
  if($widthratio<$heightratio)
   $ratio = $widthratio;
  else
   $ratio = $heightratio;
  }
  if($resizewidth_tag && !$resizeheight_tag)
  $ratio = $widthratio;
  if($resizeheight_tag && !$resizewidth_tag)
  $ratio = $heightratio;
  $newwidth = $pic_width * $ratio;
  $newheight = $pic_height * $ratio;
  if(function_exists("imagecopyresampled"))
  {
  $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数
   imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数
  }
  else
  {
  $newim = imagecreate($newwidth,$newheight);
   imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  }
  $name = $name.$filetype;
  imagejpeg($newim,$name);
  imagedestroy($newim);
 }
 else
 {
  $name = $name.$filetype;
  imagejpeg($im,$name);
 }
 }
//使用方法:
$im=imagecreatefromjpeg("./20140416103023202.jpg");//参数是图片的存方路径
$maxwidth="600";//设置图片的最大宽度
$maxheight="400";//设置图片的最大高度
$name="123";//图片的名称,随便取吧
$filetype=".jpg";//图片类型
resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用上面的函数

以上是php 縮放圖片實例簡介的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn