To parse two functions in PHP to zoom the picture, add a watermark to the picture _php tips

Source: Internet
Author: User
Tags imagejpeg

There are two ways to change the size of an image.
(1): the imagecopyresized () function is valid in all versions of GD, but its algorithm for scaling images is rather coarse.
(2): imagecopyresampled (), its pixel interpolation algorithm to get the edge of the image is relatively smooth. good quality (but the function is slower than imagecopyresized ()).
The parameters of two functions are the same.
Imagecopyresampled (DEST,SRC,DX,DY,SX,SY,DW,DH,SW,SH);
Imagecopyresized (Dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
Each of these two captures a specific location (Sx,sy) from the original image (source) to copy the image Qu region to the target T image (destination) (Dx,dy). Additionally DW,DH specifies the size of the copied image area on the target image, Sw,sh specifies the size of the image area that is copied from the original image. If you have PS experience, it is equivalent to select an area in the original image, cut to move to the destination image, with stretching or narrowing of the operation.
Example one:
(This example is to display the picture by 4/1 of its original size)

Copy Code code as follows:

<?php
Specify file path and zoom scale
$filename = ' test.jpg ';
$percent = 0.5;
Specify header file content Typezhi value
Header (' Content-type:image/jpeg ');
Get the width and height of a picture
List ($width, $height) = getimagesize ($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
Create a picture. Receive parameters are wide and high, returning the generated resource handle
$thumb = Imagecreatetruecolor ($newwidth, $newheight);
Gets the source file resource handle. Receive parameter is picture path, return handle
$source = Imagecreatefromjpeg ($filename);
Cut all fields from the source file and drop it on the target picture. 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 Picture tool SimpleImage, reference http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
How to use:
Copy Code code as follows:

<?php
Include (' simpleimage.php ');
$image = new SimpleImage ();
$image->load (' picture.jpg ');
$image->resize (250,400);
$image->save (' picture2.jpg ');? >
Set width, equal scaling
<?php
Include (' simpleimage.php ');
$image = new SimpleImage ();
$image->load (' picture.jpg ');
$image->resizetowidth (250);
$image->save (' picture2.jpg ');? >
Set height, equal proportional scaling
<?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 ');? >
Output directly to screen after zooming
<?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 beginning of the article link, to download
--------------------------------------------------------------------------------
Add a watermark to a picture
Copy Code code as follows:

<?php
$source =imagecreatefromjpeg (' e:/image/guide_pic.jpg ');
$thumb =imagecreatefromjpeg (' e:/image/l.jpg ');
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/logo.jpg ');
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.