summaryrefslogtreecommitdiff
path: root/inc/media.php
diff options
context:
space:
mode:
authorMichael Klier <chi@chimeric.de>2008-10-26 14:58:33 +0100
committerMichael Klier <chi@chimeric.de>2008-10-26 14:58:33 +0100
commit13c08e2f99078e29611f641340c2ef78eaf6f560 (patch)
tree6eb7f83db7773cf75924f73e418975bb9aa88adc /inc/media.php
parent26ece5a719ce2b1307a6af5bcdef8b9177e0dff7 (diff)
downloadrpg-13c08e2f99078e29611f641340c2ef78eaf6f560.tar.gz
rpg-13c08e2f99078e29611f641340c2ef78eaf6f560.tar.bz2
moved crop/resize functions out of fetch.php for reusability
darcs-hash:20081026135833-23886-a9c8e910571c6e7a4e1603a2ebd365b3ed37108a.gz
Diffstat (limited to 'inc/media.php')
-rw-r--r--inc/media.php305
1 files changed, 305 insertions, 0 deletions
diff --git a/inc/media.php b/inc/media.php
index 0da8a8578..3e54db512 100644
--- a/inc/media.php
+++ b/inc/media.php
@@ -721,3 +721,308 @@ function media_nstree_li($item){
return '<li class="'.$class.'">'.
'<img src="'.$img.'" alt="'.$alt.'" />';
}
+
+/**
+ * Resizes the given image to the given size
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function media_resize_image($file, $ext, $w, $h=0){
+ global $conf;
+
+ $info = @getimagesize($file); //get original size
+ if($info == false) return $file; // that's no image - it's a spaceship!
+
+ if(!$h) $h = round(($w * $info[1]) / $info[0]);
+
+ // we wont scale up to infinity
+ if($w > 2000 || $h > 2000) return $file;
+
+ //cache
+ $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
+ $mtime = @filemtime($local); // 0 if not exists
+
+ if( $mtime > filemtime($file) ||
+ media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
+ media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
+ if($conf['fperm']) chmod($local, $conf['fperm']);
+ return $local;
+ }
+ //still here? resizing failed
+ return $file;
+}
+
+/**
+ * Crops the given image to the wanted ratio, then calls media_resize_image to scale it
+ * to the wanted size
+ *
+ * Crops are centered horizontally but prefer the upper third of an vertical
+ * image because most pics are more interesting in that area (rule of thirds)
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function media_crop_image($file, $ext, $w, $h=0){
+ global $conf;
+
+ if(!$h) $h = $w;
+ $info = @getimagesize($file); //get original size
+ if($info == false) return $file; // that's no image - it's a spaceship!
+
+ // calculate crop size
+ $fr = $info[0]/$info[1];
+ $tr = $w/$h;
+ if($tr >= 1){
+ if($tr > $fr){
+ $cw = $info[0];
+ $ch = (int) $info[0]/$tr;
+ }else{
+ $cw = (int) $info[1]*$tr;
+ $ch = $info[1];
+ }
+ }else{
+ if($tr < $fr){
+ $cw = (int) $info[1]*$tr;
+ $ch = $info[1];
+ }else{
+ $cw = $info[0];
+ $ch = (int) $info[0]/$tr;
+ }
+ }
+ // calculate crop offset
+ $cx = (int) ($info[0]-$cw)/2;
+ $cy = (int) ($info[1]-$ch)/3;
+
+ //cache
+ $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext);
+ $mtime = @filemtime($local); // 0 if not exists
+
+ if( $mtime > filemtime($file) ||
+ media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
+ media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
+ if($conf['fperm']) chmod($local, $conf['fperm']);
+ return media_resize_image($local,$ext, $w, $h);
+ }
+
+ //still here? cropping failed
+ return media_resize_image($file,$ext, $w, $h);
+}
+
+/**
+ * Download a remote file and return local filename
+ *
+ * returns false if download fails. Uses cached file if available and
+ * wanted
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
+ */
+function media_get_from_URL($url,$ext,$cache){
+ global $conf;
+
+ // if no cache or fetchsize just redirect
+ if ($cache==0) return false;
+ if (!$conf['fetchsize']) return false;
+
+ $local = getCacheName(strtolower($url),".media.$ext");
+ $mtime = @filemtime($local); // 0 if not exists
+
+ //decide if download needed:
+ if( ($mtime == 0) || // cache does not exist
+ ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired
+ ){
+ if(media_image_download($url,$local)){
+ return $local;
+ }else{
+ return false;
+ }
+ }
+
+ //if cache exists use it else
+ if($mtime) return $local;
+
+ //else return false
+ return false;
+}
+
+/**
+ * Download image files
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function media_image_download($url,$file){
+ global $conf;
+ $http = new DokuHTTPClient();
+ $http->max_bodysize = $conf['fetchsize'];
+ $http->timeout = 25; //max. 25 sec
+ $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
+
+ $data = $http->get($url);
+ if(!$data) return false;
+
+ $fileexists = @file_exists($file);
+ $fp = @fopen($file,"w");
+ if(!$fp) return false;
+ fwrite($fp,$data);
+ fclose($fp);
+ if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
+
+ // check if it is really an image
+ $info = @getimagesize($file);
+ if(!$info){
+ @unlink($file);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * resize images using external ImageMagick convert program
+ *
+ * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
+ global $conf;
+
+ // check if convert is configured
+ if(!$conf['im_convert']) return false;
+
+ // prepare command
+ $cmd = $conf['im_convert'];
+ $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
+ if ($ext == 'jpg' || $ext == 'jpeg') {
+ $cmd .= ' -quality '.$conf['jpg_quality'];
+ }
+ $cmd .= " $from $to";
+
+ @exec($cmd,$out,$retval);
+ if ($retval == 0) return true;
+ return false;
+}
+
+/**
+ * crop images using external ImageMagick convert program
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
+ global $conf;
+
+ // check if convert is configured
+ if(!$conf['im_convert']) return false;
+
+ // prepare command
+ $cmd = $conf['im_convert'];
+ $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y;
+ if ($ext == 'jpg' || $ext == 'jpeg') {
+ $cmd .= ' -quality '.$conf['jpg_quality'];
+ }
+ $cmd .= " $from $to";
+
+ @exec($cmd,$out,$retval);
+ if ($retval == 0) return true;
+ return false;
+}
+
+/**
+ * resize or crop images using PHP's libGD support
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Sebastian Wienecke <s_wienecke@web.de>
+ */
+function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
+ global $conf;
+
+ if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
+
+ // check available memory
+ if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
+ return false;
+ }
+
+ // create an image of the given filetype
+ if ($ext == 'jpg' || $ext == 'jpeg'){
+ if(!function_exists("imagecreatefromjpeg")) return false;
+ $image = @imagecreatefromjpeg($from);
+ }elseif($ext == 'png') {
+ if(!function_exists("imagecreatefrompng")) return false;
+ $image = @imagecreatefrompng($from);
+
+ }elseif($ext == 'gif') {
+ if(!function_exists("imagecreatefromgif")) return false;
+ $image = @imagecreatefromgif($from);
+ }
+ if(!$image) return false;
+
+ if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
+ $newimg = @imagecreatetruecolor ($to_w, $to_h);
+ }
+ if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
+ if(!$newimg){
+ imagedestroy($image);
+ return false;
+ }
+
+ //keep png alpha channel if possible
+ if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
+ imagealphablending($newimg, false);
+ imagesavealpha($newimg,true);
+ }
+
+ //keep gif transparent color if possible
+ if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
+ if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
+ $transcolorindex = @imagecolortransparent($image);
+ if($transcolorindex >= 0 ) { //transparent color exists
+ $transcolor = @imagecolorsforindex($image, $transcolorindex);
+ $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
+ @imagefill($newimg, 0, 0, $transcolorindex);
+ @imagecolortransparent($newimg, $transcolorindex);
+ }else{ //filling with white
+ $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
+ @imagefill($newimg, 0, 0, $whitecolorindex);
+ }
+ }else{ //filling with white
+ $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
+ @imagefill($newimg, 0, 0, $whitecolorindex);
+ }
+ }
+
+ //try resampling first
+ if(function_exists("imagecopyresampled")){
+ if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
+ imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
+ }
+ }else{
+ imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
+ }
+
+ $okay = false;
+ if ($ext == 'jpg' || $ext == 'jpeg'){
+ if(!function_exists('imagejpeg')){
+ $okay = false;
+ }else{
+ $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
+ }
+ }elseif($ext == 'png') {
+ if(!function_exists('imagepng')){
+ $okay = false;
+ }else{
+ $okay = imagepng($newimg, $to);
+ }
+ }elseif($ext == 'gif') {
+ if(!function_exists('imagegif')){
+ $okay = false;
+ }else{
+ $okay = imagegif($newimg, $to);
+ }
+ }
+
+ // destroy GD image ressources
+ if($image) imagedestroy($image);
+ if($newimg) imagedestroy($newimg);
+
+ return $okay;
+}
+