国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現在服務器端調整圖片大小的方法

2024-05-04 23:36:15
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現在服務器端調整圖片大小的方法,實例分析了imageResizer與loadimage操作圖片的相關技巧,需要的朋友可以參考下

本文實例講述了php實現在服務器端調整圖片大小的方法。分享給大家供大家參考。具體分析如下:

在服務器端完成圖片大小的調整,會比在瀏覽器的處理有很多的好處。

本文介紹了PHP如何在服務器端調整圖片大小。

代碼包括兩部分:

① imageResizer() is used to process the image

② loadimage() inserts the image url in a simpler format

 

 
  1. <?php 
  2. function imageResizer($url$width$height) { 
  3. header('Content-type: image/jpeg'); 
  4. list($width_orig$height_orig) = getimagesize($url); 
  5. $ratio_orig = $width_orig/$height_orig
  6. if ($width/$height > $ratio_orig) { 
  7. $width = $height*$ratio_orig
  8. else { 
  9. $height = $width/$ratio_orig
  10. // This resamples the image 
  11. $image_p = imagecreatetruecolor($width$height); 
  12. $image = imagecreatefromjpeg($url); 
  13. imagecopyresampled($image_p$image, 0, 0, 0, 0, $width$height$width_orig$height_orig); 
  14. // Output the image 
  15. imagejpeg($image_p, null, 100); 
  16. //works with both POST and GET 
  17. $method = $_SERVER['REQUEST_METHOD']; 
  18. if ($method == 'GET') { 
  19. imageResize($_GET['url'], $_GET['w'], $_GET['h']); 
  20. elseif ($method == 'POST') { 
  21. imageResize($_POST['url'], $_POST['w'], $_POST['h']); 
  22. // makes the process simpler 
  23. function loadImage($url$width$height){ 
  24. echo 'image.php?url=', urlencode($url) , 
  25. '&w=',$width
  26. '&h=',$height
  27. ?> 

用法:

 

 
  1. //Above code would be in a file called image.php. 
  2. //Images would be displayed like this: 
  3. <img src="<?php loadImage('image.jpg', 50, 50) ?>" alt="" /> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 余干县| 垫江县| 临夏县| 周至县| 苍溪县| 远安县| 郑州市| 习水县| 隆林| 新余市| 三原县| 淮安市| 渝中区| 中超| 合阳县| 新蔡县| 西峡县| 涟水县| 石泉县| 大石桥市| 宁河县| 胶州市| 鸡东县| 瑞安市| 峨眉山市| 芷江| 金坛市| 安达市| 凤台县| 双鸭山市| 太和县| 阿巴嘎旗| 安顺市| 全南县| 东阿县| 井冈山市| 陈巴尔虎旗| 博罗县| 射阳县| 张家港市| 浦北县|