XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
resize.php
Go to the documentation of this file.
1 <?php
2 /*
3  +--------------------------------------------------------------------------------------------+
4  | DISCLAIMER - LEGAL NOTICE - |
5  +--------------------------------------------------------------------------------------------+
6  | |
7  | This program is free for non comercial use, see the license terms available at |
8  | http://www.francodacosta.com/licencing/ for more information |
9  | |
10  | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
11  | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12  | |
13  | USE IT AT YOUR OWN RISK |
14  | |
15  | |
16  +--------------------------------------------------------------------------------------------+
17 
18 */
31  function resize(phmagick $p , $width, $height = 0, $exactDimentions = false){
32  $modifier = $exactDimentions ? '!' : '>';
33 
34  //if $width or $height == 0 then we want to resize to fit one measure
35  //if any of them is sent as 0 resize will fail because we are trying to resize to 0 px
36  $width = $width == 0 ? '' : $width ;
37  $height = $height == 0 ? '' : $height ;
38 
39  $cmd = $p->getBinary('convert');
40  $cmd .= ' -scale "'. $width .'x'. $height . $modifier ;
41  $cmd .= '" -quality '. $p->getImageQuality() ;
42  $cmd .= ' -strip ';
43  $cmd .= ' "' . $p->getSource() .'" "'. $p->getDestination().'"';
44 
45 
46  $p->execute($cmd);
47  $p->setSource($p->getDestination());
48  $p->setHistory($p->getDestination());
49  return $p ;
50  }
51 
58  function resizeExactly(phmagick $p , $width, $height){
59  //requires Crop plugin
60  //requires dimensions plugin
61 
62  $p->requirePlugin('crop');
63  $p->requirePlugin('info');
64 
65  list($w,$h) = $p->getInfo($p->getSource());
66 
67  if($w > $h){
68  $h = $height;
69  $w = 0;
70  }else{
71  $h = 0;
72  $w = $width;
73  }
74 
75  $p->resize($w, $h)->crop($width, $height);
76 
77  }
78 
90  function onTheFly(phmagick $p,$imageUrl, $width, $height, $exactDimentions = false, $webPath = '', $physicalPath=''){
91  //convert web path to physical
92  $basePath = str_replace($webPath,$physicalPath, dirname($imageUrl) );
93  $sourceFile = $basePath .'/'. basename($imageUrl); ;
94 
95  //naming the new thumbnail
96  $thumbnailFile = $basePath . '/'.$width . '_' . $height . '_' . basename($imageUrl) ;
97 
98  $P->setSource($sourceFile);
99  $p->setDestination($thumbnailFile);
100 
101  if (! file_exists($thumbnailFile)){
102  $p->resize($p,$width, $height, $exactDimentions);
103  }
104 
105  if (! file_exists($thumbnailFile)){
106  //if there was an error, just use original file
107  $thumbnailFile = $sourceFile;
108  }
109 
110  //returning the thumbnail url
111  return str_replace($physicalPath, $webPath, $thumbnailFile );
112 
113  }
114 }
115 ?>