php圖片裁剪與縮放,php壓縮圖片大小

本文目錄一覽:

我要用PHP做一個縮放、裁剪後台圖片的頁面,該如何做?

按照我的理解應該是這樣的 ,首先你的伺服器上面有很多圖片,但是圖片尺寸不符合使用要求,你想給一個cms使用者製作一個可以裁減縮放圖片的功能.

你的思路前面2步驟是沒有問題的 .

但是第三個步驟是不對的

我們平常所看到的裁減功能.例如BAI度的頭像設置.是用JS模擬一個框框來模擬裁減.只有當用戶真正點擊保存後.通過js記錄了裁減的起始位置.結束位置,寬高.等參數.通過php的GD庫來重新繪製這張圖片.保存並替換掉原來的 ,並且更新資料庫信息.知道用戶點擊保存之前.這張圖片的數據源都沒有發生變化.

php求助圖片縮放裁切問題

這段代碼可以通過自已選擇來決定圖片的大小!

效果圖如下所示:希望對你有幫助!

其中

minSize: [48,48],

setSelect: [0,0,190,190],

是調整選取範圍的大小,若你 調整為120和160就改為了

setSelect: [0,0,120,160],

就可以了!

?php

error_reporting(7);

date_default_timezone_set(“Asia/Shanghai”);

header(“Content-type:text/html; Charset=utf-8”);

require_once(“./image.class.php”);

$images = new Images(“file”);

if ($_GET[‘act’] == ‘cut’){ 

 $image = “0000.jpg”;

 $res = $images-thumb($image,false,1);

 if($res == false){

  echo “裁剪失敗”;

 }elseif(is_array($res)){

  echo ‘img src=”‘.$res[‘big’].'” style=”margin:10px;”‘;

  echo ‘img src=”‘.$res[‘small’].'” style=”margin:10px;”‘;

 }elseif(is_string($res)){

  echo ‘img src=”‘.$res.'”‘;

 }

}elseif(isset($_GET[‘act’])  $_GET[‘act’] == “upload”){

 

 $path = $images-move_uploaded();

 $images-thumb($path,false,0);       //文件比規定的尺寸大則生成縮略圖,小則保持原樣

 if($path == false){

  $images-get_errMsg();

 }else{

  echo “上傳成功!a href='”.$path.”‘ target=’_blank’查看/a”;

 }

}else{

?

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “;quot;;

html

 head

  meta name=”Author” content=”SeekEver”

  meta name=”Keywords” content=””

  meta name=”Description” content=””

  meta content=”text/html; charset=UTF-8″ http-equiv=”Content-Type”

  script src=”./js/jquery.min.js” type=”text/javascript”/script

  script src=”./js/jquery.Jcrop.js” type=”text/javascript”/script

  link rel=”stylesheet” href=”./css/jquery.Jcrop.css” type=”text/css” /

script type=”text/javascript”

  jQuery(function($){

    // Create variables (in this scope) to hold the API and image size

      var jcrop_api, boundx, boundy;

     

      $(‘#target’).Jcrop({

  minSize: [48,48],

  setSelect: [0,0,190,190],

        onChange: updatePreview,

        onSelect: updatePreview,

  onSelect: updateCoords,

        aspectRatio: 1

      },

 function(){

        // Use the API to get the real image size

        var bounds = this.getBounds();

        boundx = bounds[0];

        boundy = bounds[1];

        // Store the API in the jcrop_api variable

        jcrop_api = this;

    });

 function updateCoords(c)

 {

  $(‘#x’).val(c.x);

  $(‘#y’).val(c.y);

  $(‘#w’).val(c.w);

  $(‘#h’).val(c.h);

 };

 function checkCoords()

 {

  if (parseInt($(‘#w’).val())) return true;

  alert(‘Please select a crop region then press submit.’);

  return false;

 };

      function updatePreview(c){

        if (parseInt(c.w)  0)

        {

          var rx = 48 / c.w;  //小頭像預覽Div的大小

          var ry = 48 / c.h;

        $(‘#preview’).css({

            width: Math.round(rx * boundx) + ‘px’,

            height: Math.round(ry * boundy) + ‘px’,

            marginLeft: ‘-‘ + Math.round(rx * c.x) + ‘px’,

            marginTop: ‘-‘ + Math.round(ry * c.y) + ‘px’

          });

        }

     {

          var rx = 199 / c.w;  //大頭像預覽Div的大小

          var ry = 199 / c.h;

          $(‘#preview2’).css({

            width: Math.round(rx * boundx) + ‘px’,

            height: Math.round(ry * boundy) + ‘px’,

            marginLeft: ‘-‘ + Math.round(rx * c.x) + ‘px’,

            marginTop: ‘-‘ + Math.round(ry * c.y) + ‘px’

          });

        }

      };

    });

/script

 /head

 body

 form method=”post” action=”?act=upload” enctype=”multipart/form-data”

  input type=”file” name=”file”

  input type=”submit” value=”上傳”

 /form

 div style=”float:left;”img id=”target” src=”0000.jpg”  /div

 div style=”width:48px;height:48px;margin:10px;overflow:hidden; float:left;”img  style=”float:left;” id=”preview” src=”0000.jpg” /div

 div style=”width:190px;height:195px;margin:10px;overflow:hidden; float:left;”img  style=”float:left;” id=”preview2″ src=”0000.jpg” /div

 form action=”index.php?act=cut” method=”post” onsubmit=”return checkCoords();”

  input type=”hidden” id=”x” name=”x” /

  input type=”hidden” id=”y” name=”y” /

  input type=”hidden” id=”w” name=”w” /

  input type=”hidden” id=”h” name=”h” /

  input type=”submit” value=”裁剪” /

 /form

 /body

/html

?php

?

如何實現PHP圖片裁剪與縮放

給你段代碼吧。上邊的是具體的事務處理。下面的是類文件。

////////////////////////////////////////////////////下面開始處理圖片壓縮問題

$src = “$fileurl”;

echo $src;

$image = new Image($src);

$width= $image-getimgwidth();

echo $width.”寬度是這些”;

if($width1024){

$coefficient=number_format(1024/$width, 4, ‘.’, ”);

echo $coefficient;

$image-percent = $coefficient;

$image-openImage();

$image-thumpImage();

//************************************重新給圖片命名

$randname=date(“Y”).date(“m”).date(“d”).date(“H”).date(“i”).date(“s”).rand(100, 999).”.”.$hz;

echo “br/重新命名是這個”.$randname;

$fileurl=$fileimgweb.$randname;//重新給資料庫里存的圖片地址命名

// $image-showImage();

$image-saveImage($fileurl);

}

////////////////////////////////////////////////////圖片壓縮問題處理結束

————————————————————————————–

?php

/**

圖片壓縮操作類

v1.0

*/

class Image{

private $src;

private $imageinfo;

private $image;

public $percent = 0.5;

public function __construct($src){

$this-src = $src;

}

/**

獲取圖片的寬度並傳輸給前台

*/

public function getimgwidth(){

$imgwidth= getimagesize($this-src)[0];

// echo $imgwidth;

return $imgwidth;

}

/**

打開圖片

*/

public function openImage(){

list($width, $height, $type, $attr) = getimagesize($this-src);

$this-imageinfo = array(

‘width’=$width,

‘height’=$height,

‘type’=image_type_to_extension($type,false),

‘attr’=$attr

);

$fun = “imagecreatefrom”.$this-imageinfo[‘type’];

$this-image = $fun($this-src);

}

/**

操作圖片

*/

public function thumpImage(){

$new_width = $this-imageinfo[‘width’] * $this-percent;

$new_height = $this-imageinfo[‘height’] * $this-percent;

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

//將原圖複製帶圖片載體上面,並且按照一定比例壓縮,極大的保持了清晰度

imagecopyresampled($image_thump,$this-image,0,0,0,0,$new_width,$new_height,$this-imageinfo[‘width’],$this-imageinfo[‘height’]);

imagedestroy($this-image);

$this-image = $image_thump;

}

/**

輸出圖片

*/

public function showImage(){

header(‘Content-Type: image/’.$this-imageinfo[‘type’]);

$funcs = “image”.$this-imageinfo[‘type’];

$funcs($this-image);

}

/**

保存圖片到硬碟

*/

public function saveImage($fileurl){

imagejpeg($this-image, $fileurl,75);

// $funcs = “image”.$this-imageinfo[‘type’];

// $funcs($this-image,$name.’.’.$this-imageinfo[‘type’]);

}

/**

銷毀圖片

*/

public function destruct(){

imagedestroy($this-image);

}

}

?

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/219911.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-09 11:02
下一篇 2024-12-09 11:02

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • 用Python繪製酷炫圖片

    在本篇文章中,我們將展示如何使用Python繪製酷炫的圖片。 一、安裝Python繪圖庫 在使用Python繪製圖片之前,我們需要先安裝Python繪圖庫。Python有很多繪圖庫…

    編程 2025-04-29
  • 使用axios獲取返回圖片

    使用axios獲取返回圖片是Web開發中很常見的需求。本文將介紹如何使用axios獲取返回圖片,並從多個方面進行詳細闡述。 一、安裝axios 使用axios獲取返回圖片前,首先需…

    編程 2025-04-29
  • Python 圖片轉表格

    本文將詳細介紹如何使用Python將圖片轉為表格。大家平時在處理一些資料的時候難免會遇到圖片轉表格的需求。比如從PDF文檔中提取表格等場景。當然,這個功能也可以通過手動複製、粘貼,…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Python緩存圖片的處理方式

    本文將從多個方面詳細闡述Python緩存圖片的處理方式,包括緩存原理、緩存框架、緩存策略、緩存更新和緩存清除等方面。 一、緩存原理 緩存是一種提高應用程序性能的技術,在網路應用中流…

    編程 2025-04-29
  • Python如何抓取圖片數據

    Python是一門強大的編程語言,能夠輕鬆地進行各種數據抓取與處理。抓取圖片數據是一個非常常見的需求。在這篇文章中,我們將從多個方面介紹Python如何抓取圖片數據。 一、使用ur…

    編程 2025-04-29
  • Python利用Image加圖片的方法

    在Python中,利用Image庫可以快速處理圖片,並加入需要的圖片,本文將從多個方面詳細闡述這個操作。 一、Image庫的安裝和基礎操作 首先,我們需要在Python中安裝Ima…

    編程 2025-04-28
  • Avue中如何按照後端返回的鏈接顯示圖片

    Avue是一款基於Vue.js、Element-ui等技術棧的可視化開發框架,能夠輕鬆搭建前端頁面。在開發中,我們使用到的圖片通常都是存儲在後端伺服器上的,那麼如何使用Avue來展…

    編程 2025-04-28
  • 使用CKSlide實現圖片輪播

    CKSlide是一個基於jQuery的插件,可以方便地為網頁添加幻燈片和圖片輪播效果。使用CKSlide可以讓網站更加生動、活潑,給用戶帶來更好的體驗。 一、CKSlide基本用法…

    編程 2025-04-28

發表回復

登錄後才能評論