在我们的工作的项目中,有时候我们需要显示规定尺寸的图片,虽然可以通过css来控制显示大小。但是如果图片过大,会造成加载的延迟,影响网站整体性能。因此,我们需要一个服务器来帮助我们进行图片的裁剪。流程大致是,首先我们传给服务器原图像和裁剪的尺寸,然后服务器进行裁剪,生成对应的裁剪图片,下次我们再访问相同图像和相同的裁剪尺寸的时候,我们就不需要裁剪,直接进行图片的访问就行。
Talk is cheap, show me the code.
<?php // ①构建图片请求地址比如 http://xxx.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png // ②配置nginx重写规则 rewrite /s/(.*)/(\d+)x(\d+)-(\d)/(.*) /s/resize.php?site=$1&width=$2&height=$3&mode=$4&path=$5 last; //③进行裁剪图片的处理 $path = trim($_GET['path']); $mode = intval($_GET['mode']); $site = trim($_GET['site']); $width = intval($_GET['width']); $height = intval($_GET['height']); $site_list = array('crop' => '.'); $orig_dir = dirname(__FILE__); if (!array_key_exists($site, $site_list)) { header('HTTP/1.1 400 Bad Request'); exit(); } if ($mode > 3 || $mode < 0) { header('HTTP/1.1 400 Bad Request'); exit(); } $orig_file = $site_list[$site] . $path; if (!file_exists($orig_file)) { header('HTTP/1.1 404 Not Found'); exit(); } $file_ext = '.' . pathinfo($path, PATHINFO_EXTENSION); $file_name = basename($path, $file_ext); $save_path = "{$orig_dir}/{$site}/{$width}x{$height}-{$mode}{$path}"; $save_dir = dirname($save_path); if (!file_exists($save_dir)) { wpx_mkdir($save_dir); } $target_width = $width; $target_height = $height; $save_image = $save_dir . '/' . $file_name . '.jpg'; if (file_exists($save_image)) { header('Content-Type: image/jpeg'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); echo file_get_contents($save_image); } imagecropper2($orig_file, $target_width, $target_height, $save_image); die; //原图像对应缩放裁剪,会拉伸图片 function imagecropper2($source_path, $width, $height, $save_image) { //获取原图像$filename的宽度$width_orig和高度$height_orig $info = getimagesize($source_path); $width_orig = $info[0]; $height_orig = $info[1]; $mime = $info['mime']; //根据参数$width和$height值,换算出等比例缩放的高度和宽度 if ($width && ($width_orig<$height_orig)){ $width = ($height/$height_orig)*$width_orig; }else{ $height = ($width / $width_orig)*$height_orig; } //将原图缩放到这个新创建的图片资源中 $image_p = imagecreatetruecolor($width, $height); //获取原图的图像资源 if($mime=='image/jpeg'){ $image = imagecreatefromjpeg($source_path); }elseif($mime=='image/png'){ $image = imagecreatefrompng($source_path); }elseif($mime=='image/gif'){ $image = imagecreatefromgif($source_path); } //使用imagecopyresampled()函数进行缩放设置 imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig); //将缩放后的图片$image_p保存,100(质量最佳,文件最大) if($mime=='image/jpeg'){ imagejpeg($image_p,$save_image); header('Content-Type: image/jpeg'); imagejpeg($image_p); }elseif($mime=='image/png'){ imagepng($image_p,$save_image); header('Content-Type: image/jpeg'); imagepng($image_p); }else{ imagegif($image_p,$save_image); header('Content-Type: image/jpeg'); imagegif($image_p); } } //进行比例保存裁剪,会丢失图像部分像素 function imagecropper($source_path, $target_width, $target_height, $save_image) { $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_ratio = $source_height / $source_width; $target_ratio = $target_height / $target_width; // 源图过高 if ($source_ratio > $target_ratio) { $cropped_width = $source_width; $cropped_height = $source_width * $target_ratio; $source_x = 0; $source_y = ($source_height – $cropped_height) / 2; } // 源图过宽 elseif ($source_ratio < $target_ratio) { $cropped_width = $source_height / $target_ratio; $cropped_height = $source_height; $source_x = ($source_width – $cropped_width) / 2; $source_y = 0; } // 源图适中 else { $cropped_width = $source_width; $cropped_height = $source_height; $source_x = 0; $source_y = 0; } switch ($source_mime) { case 'image/gif': $source_image = imagecreatefromgif($source_path); break; case 'image/jpeg': $source_image = imagecreatefromjpeg($source_path); break; case 'image/png': $source_image = imagecreatefrompng($source_path); break; default: return false; break; } $target_image = imagecreatetruecolor($target_width, $target_height); $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); // 裁剪 $bool = imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); // 缩放 $bool = imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); imagejpeg($target_image, $save_image); header('Content-Type: image/jpeg'); imagejpeg($target_image); imagedestroy($source_image); imagedestroy($target_image); imagedestroy($cropped_image); } // 循环生成目录 function wpx_mkdir($dir, $mode = 0777) { if (is_dir($dir) || @mkdir($dir, $mode)) { return true; } if (!wpx_mkdir(dirname($dir), $mode)) { return false; } return @mkdir($dir, $mode); }
通过上面的处理,我们就将图片按照我们设置的尺寸进行了裁剪。我们还可以定期对裁剪图片进行清理,这样就不需要占用太多服务器空间。只有经常访问的图片才会一直保存。
更多PHP相关知识,请访问PHP中文网!
以上是php影像裁剪伺服器搭建的詳細內容。更多資訊請關注PHP中文網其他相關文章!

使用數據庫存儲會話的主要優勢包括持久性、可擴展性和安全性。 1.持久性:即使服務器重啟,會話數據也能保持不變。 2.可擴展性:適用於分佈式系統,確保會話數據在多服務器間同步。 3.安全性:數據庫提供加密存儲,保護敏感信息。

在PHP中實現自定義會話處理可以通過實現SessionHandlerInterface接口來完成。具體步驟包括:1)創建實現SessionHandlerInterface的類,如CustomSessionHandler;2)重寫接口中的方法(如open,close,read,write,destroy,gc)來定義會話數據的生命週期和存儲方式;3)在PHP腳本中註冊自定義會話處理器並啟動會話。這樣可以將數據存儲在MySQL、Redis等介質中,提升性能、安全性和可擴展性。

SessionID是網絡應用程序中用來跟踪用戶會話狀態的機制。 1.它是一個隨機生成的字符串,用於在用戶與服務器之間的多次交互中保持用戶的身份信息。 2.服務器生成並通過cookie或URL參數發送給客戶端,幫助在用戶的多次請求中識別和關聯這些請求。 3.生成通常使用隨機算法保證唯一性和不可預測性。 4.在實際開發中,可以使用內存數據庫如Redis來存儲session數據,提升性能和安全性。

在無狀態環境如API中管理會話可以通過使用JWT或cookies來實現。 1.JWT適合無狀態和可擴展性,但大數據時體積大。 2.Cookies更傳統且易實現,但需謹慎配置以確保安全性。

要保護應用免受與會話相關的XSS攻擊,需採取以下措施:1.設置HttpOnly和Secure標誌保護會話cookie。 2.對所有用戶輸入進行輸出編碼。 3.實施內容安全策略(CSP)限制腳本來源。通過這些策略,可以有效防護會話相關的XSS攻擊,確保用戶數據安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显著提升应用在高并发环境下的效率。

theSession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceisesneededeededeedeedeededto toavoidperformance andunununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函數配置會話名稱。具體步驟如下:1.使用session_name()函數設置會話名稱,例如session_name("my_session")。 2.在設置會話名稱後,調用session_start()啟動會話。配置會話名稱可以避免多應用間的會話數據衝突,並增強安全性,但需注意會話名稱的唯一性、安全性、長度和設置時機。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

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