SoFunction
Updated on 2025-03-10

Analyze two functions to scale images in PHP and add watermarks to the images

There are two ways to change the size of the image.
(1): The ImageCopyResized() function is valid in all GD versions, but its algorithm for scaling images is relatively rough.
(2): ImageCopyResampled(), the image edges obtained by its pixel interpolation algorithm are relatively smooth and have better quality (but the speed of this function is slower than ImageCopyResized()).
The parameters of the two functions are the same. As follows:
ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh
);
Both of them are grabbing a specific position (sx,sy) from the original image (source) to a specific position (dx,dy) of the target image (destination). In addition, dw, dh specifies the size of the copied image area on the target image, sw, and sh specifies the size of the image area copied from the original image. If you have ps experience, it is equivalent to selecting an area in the original image, cutting and moving it to the target image, and stretching or shrinking operations.
Example 1:
(This example shows the image as 4/1 of its original size)

Copy the codeThe code is as follows:

<?php
// Specify file path and scaling
$filename = '';
$percent = 0.5;
// Specify the content typezhi value of the header file
header('Content-type: image/jpeg');
// Get the width and height of the picture
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Create a picture. The received parameters are width and height respectively, and the generated resource handle is returned.
$thumb = imagecreatetruecolor($newwidth, $newheight);
//Get the source file resource handle. The received parameter is the image path, and the handle is returned.
$source = imagecreatefromjpeg($filename);
// Cut the source file into all fields and reduce it to the target image. The first two are resource handles
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output to the browser
imagejpeg($thumb);
?>

Recommend a simple and practical zoom image tool SimpleImage, reference/blog/resizing-images-with-php/How to use:
Copy the codeThe code is as follows:

<?php
   include('');
   $image = new SimpleImage();
   $image->load('');
   $image->resize(250,400);
   $image->save('');?>
Set width, scale equally
<?php
   include('');
   $image = new SimpleImage();
   $image->load('');
   $image->resizeToWidth(250);
   $image->save('');?>
Set height, scale equally
<?php
   include('');
   $image = new SimpleImage();
   $image->load('');
   $image->resizeToHeight(500);
   $image->save('');
   $image->resizeToHeight(200);
   $image->save('');?>
Scale to 50%
<?php
   include('');
   $image = new SimpleImage();
   $image->load('');
   $image->scale(50);
   $image->save('');?>
Output directly to the screen after zooming
<?php
   header('Content-Type: image/jpeg');
   include('');
   $image = new SimpleImage();
   $image->load('');
   $image->resizeToWidth(150);
   $image->output();?>

Please click the beginning link of the source code and download it to the beginning of the article.
--------------------------------------------------------------------------------
Add watermark to the picture
Copy the codeThe code is as follows:

<?php
  $source=imagecreatefromjpeg('E:/image/guide_pic.jpg');
  $thumb=imagecreatefromjpeg('E:/image/');
//Get the width, height, type of the picture
  list($width,$height,$mine)=getimagesize('E:/image/guide_pic.jpg');
  imagecopymerge ($source,$thumb,$width-124,$height-150,0,0,88,73,70);
//Generate pictures
  imagejpeg($source,'E:/image/');
?>