Home > Backend Development > PHP Tutorial > Code example for php batch scaling of images

Code example for php batch scaling of images

WBOY
Release: 2016-07-25 08:59:18
Original
915 people have browsed it
  1. ;Translate the image format using the original image size

  2. [Translation]
  3. width=0
  4. height=0

  5. ;Stretch the image to the specified size

  6. [Stretch]
  7. width=800
  8. height=600

  9. ;Zoom the image to the specified Width with height auto size

  10. [AutoHeight]
  11. width=740
  12. height= 0

  13. ;Zoom the image to the specified Height with width auto size

  14. [AutoWidth]
  15. width=0
  16. height=380
  17. */ ?>

Copy Code

2. PHP code for zooming pictures The variable classes is an array that can select any number of settings specified in the ini file.

  1. /**
  2. * Batch bloom pictures
  3. * edit bbs.it-home.org
  4. * at 2013/5/15
  5. */
  6. $oimg = "test.jpg";//Original image name
  7. $classes = array('Translation','AutoHeight','AutoWidth ','Stretch');//Give classes for the new creating images' size which are defined in the specified
  8. ini file
  9. $suffix = 'jpg';//The new image's suffix
  10. $inifile = 'image.ini. php';
  11. $size = getimagesize($oimg);
  12. $x = $size[0]/$size[1];
  13. $name = explode('.',$oimg);
  14. if(!file_exists ($inifile)) die('Ini file does not exist!');
  15. $cn = parse_ini_file($inifile,true);//Parse the class style image size from ini file
  16. foreach($classes as $class){
  17. foreach($cn as $k=>$v){
  18. if($k==$class){
  19. if($v['width'] && $v['height']){
  20. $thumbWidth = $v['width'];
  21. $thumbHeight = $v['height'];
  22. }elseif($v['width']){
  23. $thumbWidth = $v['width'];
  24. $thumbHeight = round ($thumbWidth/$x);
  25. }elseif($v['height']){
  26. $thumbHeight = $v['height'];
  27. $thumbWidth = round($thumbHeight*$x);
  28. }else{
  29. $thumbWidth = $size[0];
  30. $thumbHeight = $size[1];
  31. }
  32. break;
  33. }
  34. }
  35. if(!isset($thumbHeight) && !isset($thumbWidth)) die('Ini file Settings error!');
  36. $nimg = $name[0].'_'.$class.'.'.$suffix;//New image file name
  37. $source = imagecreatefromjpeg($oimg);
  38. $ thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  39. imagecopyresampled($thumb,$source,0,0,0,0,$thumbWidth,$thumbHeight,$size[0],$size[1]);
  40. if ($suffix=='jpg') $method = 'imagejpeg';
  41. else $method='image'.$suffix;
  42. $method($thumb, $nimg);
  43. imagedestroy($thumb);//Release the image source
  44. imagedestroy($source);
  45. }
  46. ?>
Copy code

php has a function specifically for processing images. For some more demanding image scaling, php can also do it.

>>>> Articles you may be interested in: PHP example code for zooming images Example reference for php proportional scaling of images PHP tool for scaling images in equal proportions SimpleImage example learning



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