Parse php's two image scaling functions to add watermark to the image

  • 2020-06-15 07:51:03
  • OfStack

There are two ways to change the size of an image.
(1): The ImageCopyResized() function works in all VERSIONS of GD, but its algorithm for scaling images is rough.
(2):ImageCopyResampled(), whose pixel interpolation algorithm produces a smooth image edge with good quality (but the speed of this function is slower than ImageCopyResized())).
The parameters of the two functions are 1, 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 grab a specific location (sx,sy) from the original image (source) and copy the qu region of the image to a specific location (dx,dy) of the target t image (destination). In addition, dw,dh specifies the size of the image area copied on the target image, and sw, sh specifies the size of the image area copied from the original image. If you have experience with ps, it is equivalent to selecting 1 area in the original image, cutting and moving to the target image, while stretching or shrinking.
Case 1:
(In this example, the image is displayed at 4/1 of its original size)


<?php
//  Specifies the file path and scale 
$filename = 'test.jpg';
$percent = 0.5;
//  Specified header file Content typezhi value 
header('Content-type: image/jpeg');
//  Gets the width and height of the image 
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
//  create 1 A picture. The receive parameters are width and height, and the generated resource handle is returned 
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Gets the source file resource handle. The receive parameter is the image path and returns the handle 
$source = imagecreatefromjpeg($filename);
//  Cut all fields from the source file and zoom in on 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 tool SimpleImage images, reference http: / / www white hat - web - design. co. uk/blog/resizing - images - with - php /
Usage:

<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resize(250,400);
   $image->save('picture2.jpg');?>
 Set the width and scale accordingly 
<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToWidth(250);
   $image->save('picture2.jpg');?>
 Set the height and scale equally 
<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToHeight(500);
   $image->save('picture2.jpg');
   $image->resizeToHeight(200);
   $image->save('picture3.jpg');?>
 Scale to 50%
<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->scale(50);
   $image->save('picture2.jpg');?>
 Zoom in and out directly to the screen 
<?php
   header('Content-Type: image/jpeg');
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToWidth(150);
   $image->output();?>

SimpleImage. php source please click the link at the beginning of the article to download there
--------------------------------------------------------------------------------
Watermark the image

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


Related articles: