Example 1, JS proportional scaling of a picture.
Example 2, PHP scaling of database images:
-
class ImgSF{ - function make_img($img_address){
- //Important scaling
//Because PHP only Can operate on resources, so you need to copy the image that needs to be scaled and create it as a new resource
- $src=imagecreatefromjpeg($img_address);
//Get the width and sum of the source image Height
- $size_src=getimagesize($img_address);
- $w=$size_src['0'];
- $h=$size_src['1'];
//Specify scaling The maximum width (may also be the height)
- $max=300;
//According to the maximum value of 300, calculate the length of the other side and get 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 true color image resource of $w width and $h height
- $image=imagecreatetruecolor($w, $h);
//Key function, parameters (target resource, source, starting coordinates of the target resource x,y, starting coordinates of the source resource x,y, width and height of the target resource w,h, source The width and height of the resource are w, h)
- imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);< /p>
//Tell the browser to parse as an image
- header('content-type:image/png');
- imagepng($image);
// Destroy resources
- imagedestroy($image);
- }
- }
- $obj=new ImgSF();
- $obj->make_img("./img/IMG_20140424_200722.jpg");
-
-
Copy code
|