Code to change image size in PHP_PHP tutorial
Let me first introduce a function I wrote myself.
$imgsrc = "http://www.nowamagic .net/images/3.jpg";
$width = 780;
$height = 420;
resizejpg($imgsrc,$imgdst,$width,$height);
function resizejpg( $imgsrc,$imgdst,$imgwidth,$imgheight)
{
//$imgsrc jpg format image path $imgdst jpg format image save file name $imgwidth width to be changed $imgheight height to be changed
//Get the width and height of the image
$arr = getimagesize($imgsrc);
header("Content-type: image/jpg");
$imgWidth = $imgwidth;
$ imgHeight = $imgheight;
// Create image and define colors
$imgsrc = imagecreatefromjpeg($imgsrc);
$image = imagecreatetruecolor($imgWidth, $imgHeight); //Create a color basemap
imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
imagepng($image);
imagedestroy($image);
}
?>
imagecopyresampled
imagecopyresampled -- Resample copy part of the image and resize it.
int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
imagecopyresampled() takes a square in an image Regions are copied into another image, smoothly interpolating pixel values and thus, inter alia, reducing the size of the image while still maintaining great sharpness. dst_im and src_im are the identifiers of the target image and source image respectively. If the source and destination have different widths and heights, the image shrinks and stretches accordingly. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_im and src_im are the same), but the results are unpredictable if the regions overlap.
Note: There is a problem because of the palette image limit (255+1 colors). Resampling or filtering an image often requires more than 255 colors, and an approximation is used in calculating the new resampled pixels and their colors. When trying to assign a new color to a palette image, if that fails we choose the closest (theoretically) calculated color. This is not always the visually closest color. This can produce weird results, such as blank (or visually blank) images. To bypass this problem, use a truecolor image as the target image, such as one created with imagecreatetruecolor().
Note: imagecopyresampled() requires GD 2.0.l or higher.
A simple example:
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $ new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
Example 2:
// The file
$filename = 'test.jpg' ;
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg') ;
// Get new dimensions
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;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>
There are two ways to change the image size:
The ImageCopyResized() function is valid in all GD versions, but its algorithm for scaling images is rough.
ImageCopyResamples(), the image edges obtained by its pixel interpolation algorithm are relatively smooth. (But this function is slower than ImageCopyResized()).
The parameters of the two functions are the same, as follows:
imageCopyResampled(dest, src,dy,dx,sx,sy,dw,dh,sw,sh);
imageCopyResized(dest,src,dy,dx,sx,sy,dw,dh,sw,sh);
Example:
$src = ImageCreateFromJPEG(' php.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $widht/2;
$y = $height/ 2;
$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$widht,$height);
header('Content-Type : image/png');
ImagePNG($det);
?>
How to change jpg image file in php The size of
function resize_jpg($img, $w,$h){
// $thumb = imagecreate ($w, $h);
$image = imagecreatefromjpeg($img);
$imagedata = getimagesize($img);
if ($h = "auto") $h = $w/($imagedata[0]/$imagedata[1]);//Get the height based on the aspect ratio of the original image!
$thumb = imagecreatetruecolor ($w, $h);
imagecopyresized ($thumb, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1 ]);
imagejpeg($thumb);
}
//resize_jpg($file,$w,$h);
resize_jpg("images/dsc01244.jpg",100,100);
imagedestory($thumb);
imagedestory($image);
?>
Function code:
/*
* Image thumbnail
* $srcfile source image,
* $rate scaling ratio , the default is to reduce it by half, or the specific width pixel value
* $filename Output image file name jpg
* For example: resizeimage("zt32.gif",0.1);
* For example: resizeimage("zt32 .gif",250 );
* Note: When calling, place the result of the function directly in the SRC attribute in the IMG tag of the HTML file
*/
function resizeimage($srcfile,$rate=.5 , $filename = "" ){
$size=getimagesize($srcfile);
switch($size[2]){
case 1:
$img=imagecreatefromgif($srcfile);
break;
case 2:
$img=imagecreatefromjpeg($srcfile);
break;
case 3:
$img=imagecreatefrompng($srcfile);
break ;
default:
exit;
}
//The width and height of the source image
$srcw=imagesx($img);
$srch=imagesy($img);
//Width and height of the destination image
if($size[0] <= $rate || $size[1] <= $rate){
$dstw=$srcw;
$dsth=$srch;
}else{
if($rate <= 1){
$dstw=floor($srcw*$rate);
$dsth=floor( $srch*$rate);
}else {
$dstw=$rate;
$rate = $rate/$srcw;
$dsth=floor($srch*$rate);
}
}
//echo "$dstw,$dsth,$srcw,$srch ";
//Create a new true color image
$im=imagecreatetruecolor($dstw,$dsth );
$black=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,$dstw,$dsth,$black);
imagecopyresized($im,$img,0 ,0,0,0,$dstw,$dsth,$srcw,$srch);
// Output the image to the browser or file in JPEG format
if( $filename ) {
// Image saving and output
imagejpeg($im, $filename);
}else {
//Output the image to the browser
imagejpeg($im);
}
//Release Image
imagedestroy($im);
imagedestroy($img);
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
