使用php怎么在服务器端调整图片的大小-创新互联

这篇文章给大家介绍使用php怎么在服务器端调整图片的大小,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

成都创新互联专注于企业成都全网营销推广、网站重做改版、宝清网站定制设计、自适应品牌网站建设、html5成都做商城网站、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为宝清等各大城市提供网站开发制作服务。
<?php
 function imageResizer($url, $width, $height) {
  header('Content-type: image/jpeg');
  list($width_orig, $height_orig) = getimagesize($url);
  $ratio_orig = $width_orig/$height_orig;
  if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
  } else {
   $height = $width/$ratio_orig;
  }
  // This resamples the image
  $image_p = imagecreatetruecolor($width, $height);
  $image = imagecreatefromjpeg($url);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  // Output the image
  imagejpeg($image_p, null, 100);
 }
 //works with both POST and GET
 $method = $_SERVER['REQUEST_METHOD'];
 if ($method == 'GET') {
  imageResize($_GET['url'], $_GET['w'], $_GET['h']);
  } elseif ($method == 'POST') {
  imageResize($_POST['url'], $_POST['w'], $_POST['h']);
  }
 // makes the process simpler
 function loadImage($url, $width, $height){
  echo 'image.php?url=', urlencode($url) ,
  '&w=',$width,
  '&h=',$height;
 }
?>

用法:

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

关于使用php怎么在服务器端调整图片的大小就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

本文题目:使用php怎么在服务器端调整图片的大小-创新互联
文章链接:https://www.cdcxhl.com/article46/dosdeg.html

成都网站建设公司_创新互联,为您提供小程序开发网站建设手机网站建设建站公司品牌网站建设ChatGPT

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司