/** * 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 <*****@*****.**> */ 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); }
/** * 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 <*****@*****.**> */ 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; // check if the crop can be handled completely by resize, // i.e. the specified width & height match the aspect ratio of the source image if ($w == round($h * $fr)) { return media_resize_image($file, $ext, $w); } 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 (!empty($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); }
/** * 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 <*****@*****.**> */ 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! if ($conf['syslog']) { syslog(LOG_WARNING, '[media.php] media_crop_image: ' . $file . ', size: ' . $info[0] . 'x' . $info[1] . ', resize to:' . $w . 'x' . $h); } // calculate crop size $fr = $info[0] / $info[1]; $tr = $w / $h; // check if the crop can be handled completely by resize, // i.e. the specified width & height match the aspect ratio of the source image // check rounding for either width or height to avoid cropping for portrait photos if ($w == round($h * $fr) || $h == round($w / $fr)) { if ($conf['syslog']) { syslog(LOG_WARNING, '[media.php] media_crop_image: use media_resize_image instead: ' . $file . ', width: ' . $w); } return media_resize_image($file, $ext, $w); } if ($conf['syslog']) { syslog(LOG_WARNING, '[media.php] media_crop_image: resize is not possible and we are doing crop now'); } if ($conf['syslog']) { syslog(LOG_WARNING, '[media.php] media_crop_image: fr: ' . $fr . ' tr: ' . $tr . ' round w: ' . round($h * $fr)) . ' round h:' . round($w / $fr); } 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 (!empty($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); }