Example of database image proportional scaling in php and js

Source: Internet
Author: User

JS proportional scaling of an image

Code
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> the latest javascript automatically displays images in proportion and compressed images in proportion </title>
<Script type = "text/javascript">
Function AutoResizeImage (maxWidth, maxHeight, objImg ){
Var img = new Image ();
Img. src = objImg. src;
Var hRatio;
Var wRatio;
Var Ratio = 1;
Var w = img. width;
Var h = img. height;
WRatio = maxWidth/w;
HRatio = maxHeight/h;
If (maxWidth = 0 & maxHeight = 0 ){
Ratio = 1;
} Else if (maxWidth = 0 ){//
If (hRatio <1) Ratio = hRatio;
} Else if (maxHeight = 0 ){
If (wRatio <1) Ratio = wRatio;
} Else if (wRatio <1 | hRatio <1 ){
Ratio = (wRatio <= hRatio? WRatio: hRatio );
}
If (Ratio <1 ){
W = w * Ratio;
H = h * Ratio;
}
ObjImg. height = h;
ObjImg. width = w;
}
</Script>
</Head>
<Body>
<Br/>
Source image display (534X800) <br/>
Onload = "AutoResizeImage (0, 0, this) <br/>
<A href = ". /img/img_201401__200722.jpg "target =" _ blank "> </a> <br/>
3. Compression Based on 250x250 x <br/>
Onload = "AutoResizeImage (250,250, this)" <br/>
<A href = ". /img/img_201401__200722.jpg "target =" _ blank "> </a> <br/>
6. If the original height and width of the image are smaller than the maximum compression height and width, the image will not be displayed larger (based on the original image) <br/>
The source image is 444x207, compressed to 500x600, and the original image is displayed. <br/>
Onload = "AutoResizeImage (500,600, this)" <br/>
<A href = ". /img/img_201401__200722.jpg "target =" _ blank "> </a> <br/>
</Body>
</Html>

Php proportional scaling of database images
Copy codeThe Code is as follows:
<? Php
Class ImgSF {
Function make_img ($ img_address ){
// Proportional scaling of the image

// Because PHP can only operate on resources, you need to copy the image to be scaled and create a new resource.
$ Src = imagecreatefromjpeg ($ img_address );

// Obtain the width and height of the source Image
$ Size_src = getimagesize ($ img_address );
$ W = $ size_src ['0'];
$ H = $ size_src ['1'];

// Specify the scaled width (or the height)
$ Max = 300;

// Calculate the length of the other side based on the maximum value of 300 to obtain the scaled image width and height.
If ($ w> $ h ){
$ W = $ max;
$ H = $ h * ($ max/$ size_src ['0']);
} Else {
$ H = $ max;
$ W = $ w * ($ max/$ size_src ['1']);
}


// Declare a $ w width, $ h high real color image resource
$ Image = imagecreatetruecolor ($ w, $ h );


// Key functions, parameters (start coordinates x and y of the target resource, source resource, and target resource, and start coordinates x and y of the source resource, w, h, source resource width/height w, h)
Imagecopyresampled ($ image, $ src, 0, 0, 0, $ w, $ h, $ size_src ['0'], $ size_src ['1']);

// Tell the browser to parse the image
Header ('content-type: image/png ');
Imagepng ($ image );

// Destroy resources
Imagedestroy ($ image );
}
}
$ Obj = new ImgSF ();
$ Obj-> make_img ("./img/img_201401__200722.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.