Home > Backend Development > PHP Tutorial > PHP and JS code for proportional scaling of database images

PHP and JS code for proportional scaling of database images

WBOY
Release: 2016-07-25 09:13:05
Original
1001 people have browsed it

Example 1, JS proportional scaling of a picture.

  1. The latest javascript automatically displays images in proportion and compresses images in proportion -bbs.it-home.org

  2. Original image display (534 X 800)
  3. onload="AutoResizeImage(0,0,this)
  4. 534 X 800

  5. 3. Compress proportionally by height 250 and width 250
  6. onload="AutoResizeImage(250,250,this)"
  7. 200 The image will be enlarged and displayed (displayed according to the original image)<br /> </li>
<li>The original image is 444 x 207, compressed to 500 x 600, and the original image will be displayed<br /> </li>
<li>onload=
  8. 444 X 207

Copy code
Example 2, PHP scaling of database images:

  1. class ImgSF{

  2. function make_img($img_address){
  3. //Important scaling

  4. //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

  5. $src=imagecreatefromjpeg($img_address);

  6. //Get the width and sum of the source image Height

  7. $size_src=getimagesize($img_address);
  8. $w=$size_src['0'];
  9. $h=$size_src['1'];

  10. //Specify scaling The maximum width (may also be the height)

  11. $max=300;

  12. //According to the maximum value of 300, calculate the length of the other side and get the scaled image width and height

  13. if($w > $h){
  14. $w=$max;
  15. $h=$h*($max/$size_src['0']);
  16. }else{
  17. $h=$max;
  18. $ w=$w*($max/$size_src['1']);
  19. }
  20. //Declare a true color image resource of $w width and $h height
  21. $image=imagecreatetruecolor($w, $h);

  22. //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)

  23. imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);< /p>
  24. //Tell the browser to parse as an image

  25. header('content-type:image/png');
  26. imagepng($image);

  27. // Destroy resources

  28. imagedestroy($image);
  29. }
  30. }
  31. $obj=new ImgSF();
  32. $obj->make_img("./img/IMG_20140424_200722.jpg");

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template