Home > Backend Development > PHP Tutorial > How to modify the size of uploaded images in php

How to modify the size of uploaded images in php

墨辰丷
Release: 2023-03-31 15:46:01
Original
3187 people have browsed it

This article mainly introduces the method of changing the size of uploaded images in PHP. It involves the skills of operating images in PHP. It is of great practical value. Friends in need can refer to it.

The example in this article tells the story of changing the size of uploaded images in PHP. Methods. Share it with everyone for your reference. The specific implementation method is as follows:

<?php
// This is the temporary file created by PHP
$uploadedfile = $_FILES[&#39;uploadfile&#39;][&#39;tmp_name&#39;];
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "images/". $_FILES[&#39;uploadfile&#39;][&#39;name&#39;];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
// NOTE: PHP will clean up the temp file it created when the request
// has completed.
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

php techniques for caching through file storage

php is for uploading image files Detailed explanation of functions

php operations for reading, editing and saving files

The above is the detailed content of How to modify the size of uploaded images in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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