利用jquery Jcrop跟 php实现截图功能
利用jquery Jcrop和 php实现截图功能
项目中用到了一个上传头像的功能,需要进行无刷新的图片上传,并对上传后的图片进行用户要求的截图,无刷新上传我就不说了,用的Juploader,相信大家并不陌生,重点讲一下jcron和php配置实现图片的截取的功能,好了,言归正传。首先介绍一下jcron的用法,我就不一一解释了,我们只看最经常用的到截图功能:
<span style="font-size: 18px; ">$(function(){ $('#cropbox').Jcrop({ aspectRatio: 1, onSelect: updateCoords }); });</span>
以上是控制,对哪个图片进行截图,“cropbox”是你要截取的img对象的id,“aspectRatio”控制等比例截取,“onSelect”的值是一个方法名,在选取时调用的方法
,个参数详情解释如下:
Option Name | Value Type | Description | Default |
---|---|---|---|
aspectRatio | decimal | Aspect ratio of w/h (e.g. 1 for square) | n/a |
minSize | array [ w, h ] | Minimum width/height, use 0 for unbounded dimension | n/a |
maxSize | array [ w, h ] | Maximum width/height, use 0 for unbounded dimension | n/a |
setSelect | array [ x, y, x2, y2 ] | Set an initial selection area | n/a |
bgColor | color value | Set color of background container | 'black' |
bgOpacity | decimal 0 - 1 | Opacity of outer image when cropping | .6 |
<span style="font-size:18px;">function updateCoords(c) { $('#x').val(c.x); $('#y').val(c.y); $('#w').val(c.w); $('#h').val(c.h); };</span>
有了这个方法,可以在你截图是更新隐藏域中的坐标值,通过隐藏域把坐标信息传到后台。
<span style="font-size:18px;"><form action="crop.php" 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="Crop Image"> </form></span>
ok,到此,前台已经告一段落,我们看后台的php代码。
后台php主要是根据前台传递的坐标,对原图进行截取,支持jpg,png,和gif三种图片格式,当然,你可以扩展他,使他支持更多的图片格式。
<span style="font-size:18px;">class Img_shot { private $filename; private $ext; private $x; private $y; private $x1; private $y1; private $width = 124; private $height = 124; private $jpeg_quality = 90; /** * 构造器 * * */ public function __construct() { log_message('debug', "Img_shot Class Initialized"); } /** * 初始化截图对象 *@param filename 源文件路径全明 *@param width 截图的宽 *@param height 截图的高 *@param x 横坐标1 *@param y 纵坐标1 *@param x1 横坐标1 *@param y1 横坐标2 * */ public function initialize($filename,$x,$y,$x1,$y1) { if(file_exists($filename)) { $this->filename = $filename; $pathinfo = pathinfo($filename); $this->ext = $pathinfo['extension']; } else { $e = new Exception('the file is not exists!',1050); throw $e; } $this->x = $x; $this->y = $y; $this->x1 = $x1; $this->y1 = $y1; } /** * 生成截图 * 根据图片的格式,生成不同的截图 */ public function generate_shot() { switch($this->ext) { case 'jpg': return $this->generate_jpg(); break; case 'png': return $this->generate_png(); break; case 'gif': return $this->generate_gif(); break; default: return false; } } /** * 得到生成的截图的文件名 * */ private function get_shot_name() { $pathinfo = pathinfo($this->filename); $fileinfo = explode('.',$pathinfo['basename']); $filename = $fileinfo[0] . '_small.' . $this->ext; return $pathinfo['dirname'] . '/' .$filename; } /** * 生成jpg格式的图片 * */ private function generate_jpg() { $shot_name = $this->get_shot_name(); $img_r = imagecreatefromjpeg($this->filename); $dst_r = ImageCreateTrueColor($this->width, $this->height); imagecopyresampled($dst_r,$img_r,0,0,$this->x,$this->y, $this->width,$this->height,$this->x1,$this->y1); imagejpeg($dst_r,$shot_name,$this->jpeg_quality); return $shot_name; } /** * 生成gif格式的图片 * */ private function generate_gif() { $shot_name = $this->get_shot_name(); $img_r = imagecreatefromgif($this->filename); $dst_r = ImageCreateTrueColor($this->width, $this->height); imagecopyresampled($dst_r,$img_r,0,0,$this->x,$this->y, $this->width,$this->height,$this->x1,$this->y1); imagegif($dst_r,$shot_name); return $shot_name; } /** * 生成png格式的图片 * */ private function generate_png() { $shot_name = $this->get_shot_name(); $img_r = imagecreatefrompng($this->filename); $dst_r = ImageCreateTrueColor($this->width, $this->height); imagecopyresampled($dst_r,$img_r,0,0,$this->x,$this->y, $this->width,$this->height,$this->x1,$this->y1); imagepng($dst_r,$shot_name); return $shot_name; } } </span>
接收到前台的坐标信息后,你可以实例化该类,用来生成图片,返回生成的图片的名称,你就可以使用啦。
截完图之后:

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

在java中,private的意思為“私有的”,是一種存取控制修飾符,用於修飾類別、屬性和方法。用private修飾的類別成員,只能被該類別本身的方法存取和修改,而不能被任何其他類別(包括該類別的子類別)存取和引用;因此,private修飾符具有最高的保護等級。

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

這篇文章帶大家解讀vue原始碼,來介紹一下Vue2中為什麼可以使用 this 存取各種選項中的屬性,希望對大家有幫助!

同事因為this指向的問題卡住的bug,vue2的this指向問題,使用了箭頭函數,導致拿不到對應的props。當我跟他介紹的時候他竟然不知道,隨後也刻意的看了一下前端交流群,至今最起碼還有70%以上的前端程式設計師搞不明白,今天給大家分享一下this指向,如果啥都沒學會,請給我一個大嘴巴子。

watch4pro和gt各自具有不使用的特點和適用場景,如果注重功能的全面性、高性能和時尚外觀,同時願意承擔較高的價格,那麼Watch 4 Pro可能更適合。如果對功能要求不高,更注重電池續航力和價格的合理性,那麼GT系列可能更適合。最終的選擇應根據個人需求、預算和喜好來決定,建議在購買前仔細考慮自己的需求,並參考各種產品的評測和比較,以做出更明智的選擇。
