>  기사  >  백엔드 개발  >  PHP의 서버 측에서 이미지 크기를 조정하는 방법 소개

PHP의 서버 측에서 이미지 크기를 조정하는 방법 소개

黄舟
黄舟원래의
2017-07-26 13:48:091370검색

이 글에서는 주로 PHP에서 서버 측에서 이미지 크기를 조정하는 방법을 소개합니다. 이미지를 조작하기 위한 imageResizer 및 loadimage 관련 기술을 분석한 예시입니다. 필요한 친구들이 참고할 수 있습니다

이 글에서는 PHP 조정 예시를 설명합니다. 서버 측의 이미지 크기. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 분석은 다음과 같습니다.

서버 측에서 이미지 크기 조정을 완료하면 브라우저에서 처리하는 것보다 많은 이점이 있습니다.
이 글에서는 PHP에서 서버 측에서 이미지 크기를 조정하는 방법을 소개합니다.

코드는 두 부분으로 구성됩니다.

① imageResizer()는 이미지를 처리하는 데 사용됩니다
② loadimage()는 더 간단한 형식으로 이미지 URL을 삽입합니다


<?php
 function imageResizer($url, $width, $height) {
  header(&#39;Content-type: image/jpeg&#39;);
  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[&#39;REQUEST_METHOD&#39;];
 if ($method == &#39;GET&#39;) {
  imageResize($_GET[&#39;url&#39;], $_GET[&#39;w&#39;], $_GET[&#39;h&#39;]);
  } elseif ($method == &#39;POST&#39;) {
  imageResize($_POST[&#39;url&#39;], $_POST[&#39;w&#39;], $_POST[&#39;h&#39;]);
  }
 // makes the process simpler
 function loadImage($url, $width, $height){
  echo &#39;image.php?url=&#39;, urlencode($url) ,
  &#39;&w=&#39;,$width,
  &#39;&h=&#39;,$height;
 }
?>

사용법:


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

위 내용은 PHP의 서버 측에서 이미지 크기를 조정하는 방법 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.