This article describes the function of adding watermarks to two images in php. For more information, see
There are two ways to change the image size.
(1): The ImageCopyResized () function is valid in all GD versions, but its algorithm for scaling images is rough.
(2): ImageCopyResampled (), the edge of the image obtained by the pixel interpolation algorithm is smoother and the quality is better (but the speed of this function is slower than that of ImageCopyResized ).
The parameters of the 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 );
Both of them are from the original image (source) capture a specific location (sx, sy) copy the image qu region to the specific location (dx, dy) of the target t Image (destination ). In addition, dw and dh specify the size of the copied image area on the target image, sw and sh specify 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 to the target image, and stretching or narrowing at the same time.
Example 1:
(In this example, the image is displayed in 4/1 of the original size)
The code is as follows:
// Specify the file path and zoom ratio
$ Filename = 'test.jpg ';
$ Percent = 0.5;
// Specify the header file Content typezhi value
Header ('content-type: image/jpeg ');
// Obtain the image width and height
List ($ width, $ height) = getimagesize ($ filename );
$ Newwidth = $ width * $ percent;
$ Newheight = $ height * $ percent;
// Create an image. The received parameters are respectively width and height, and the generated resource handle is returned.
$ Thumb = imagecreatetruecolor ($ newwidth, $ newheight );
// Obtain the source file resource handle. The receiving parameter is the image path, and the return handle is
$ Source = imagecreatefromjpeg ($ filename );
// Cut all fields of the source file and narrow down the file 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 );
?>
Recommended a simple and practical image scaling tool SimpleImage, refer to http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
Usage:
The code is as follows:
Include ('simpleimage. php ');
$ Image = new SimpleImage ();
$ Image-> load('picture.jpg ');
$ Image-> resize( 250,400 );
$ Image-> save('picture2.jpg ');?>
Set width and proportional scaling
Include ('simpleimage. php ');
$ Image = new SimpleImage ();
$ Image-> load('picture.jpg ');
$ Image-> resizeToWidth (250 );
$ Image-> save('picture2.jpg ');?>
Set height, proportional scaling
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% in proportion
Include ('simpleimage. php ');
$ Image = new SimpleImage ();
$ Image-> load('picture.jpg ');
$ Image-> scale (50 );
$ Image-> save('picture2.jpg ');?>
Scale and output directly to the screen
Header ('content-Type: image/jpeg ');
Include ('simpleimage. php ');
$ Image = new SimpleImage ();
$ Image-> load('picture.jpg ');
$ Image-> resizeToWidth (150 );
$ Image-> output ();?>
SimpleImage. php source code. click the link at the beginning of the article to download it.
--------------------------------------------------------------------------------
Add watermarks to images
The code is as follows:
$ Source = imagecreatefromjpeg ('E:/image/guide_pic.jpg ');
$ Thumb = imagecreatefromjpeg ('E:/image/l. JPG ');
// Obtain the image width, height, and type.
List ($ width, $ height, $ mine) = getimagesize ('E:/image/guide_pic.jpg ');
Imagecopymerge ($ source, $ thumb, $ width-124, $ height-150, 88,73, 70 );
// Generate an image
Imagejpeg ($ source, 'E:/image/logo.jpg ');
?>