How to use imagecopyresampled (image processing function) in php?

青灯夜游
Release: 2023-04-08 14:38:02
forward
4093 people have browsed it

How to use imagecopyresampled (image processing function) in php? The following article will explain in detail how to use the PHP image processing function imagecopyresampled. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use imagecopyresampled (image processing function) in php?

php image processing function imagecopyresampled usage

Syntax

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
Copy after login

Parameters:

dst_imageTarget image connection resource.
src_imageSource image connection resource.
dst_xTarget X coordinate point.
dst_yTarget Y coordinate point.
src_xThe X coordinate point of the source.
src_yThe Y coordinate point of the source.
dst_wTarget width.
dst_hTarget height.
src_wThe width of the source image.
src_hThe height of the source image.

Returns TRUE on success, or FALSE on failure.

Case

1. Image cropping

<?php
  $targ_w = $targ_h = 150; // 设置目标宽度与高度
  $jpeg_quality = 90; // 图片质量90,满分为100
  $src = &#39;demo_files/pool.jpg&#39;; // 被处理的图片
  $img_r = imagecreatefromjpeg($src); // 获取原图
  $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); // 获取新图
  imagecopyresampled($dst_r,$img_r,0,0,$_POST[&#39;x&#39;],$_POST[&#39;y&#39;],
  $targ_w,$targ_h,$_POST[&#39;w&#39;],$_POST[&#39;h&#39;]); // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度
  header(&#39;Content-type: image/jpeg&#39;);
  imagejpeg($dst_r,null,$jpeg_quality); // 输出图象到浏览器或文件
?>
Copy after login

2. Resampling

<?php
// 源文件
$filename = &#39;1.jpg&#39;;
// 设置最大宽高
$width = 400;
$height = 400;
// Content type
header(&#39;Content-Type: image/jpeg&#39;);
// 获取新尺寸
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
  $width = $height*$ratio_orig;
} else {
  $height = $width/$ratio_orig;
}
// 重新取样
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// 输出
imagejpeg($image_p, null, 100);
?>
Copy after login

Attached are three files of the uploaded image An idea

  1. Select the picture, submit the form, the server handles the upload uniformly, save the path

  2. Select the picture, upload, get the path, Submit the form, save the path

  3. Select the image, upload it to the server, obtain the image from the server through some means, and save it locally

For more related knowledge, please pay attention to PHP Chinese website! !

The above is the detailed content of How to use imagecopyresampled (image processing function) in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template