Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create thumnbnail and return it's URL on success
- *
- * @param string $path file path
- * @param string $mime file mime type
- * @return string|false
- * @author Dmitry (dio) Levashov
- **/
- protected function createTmb($path, $stat) {
- if (!$stat || !$this->canCreateTmb($path, $stat)) {
- return false;
- }
- $name = $this->tmbname($stat);
- $tmb = $this->tmbPath.DIRECTORY_SEPARATOR.$name;
- // copy image into tmbPath so some drivers does not store files on local fs
- if (($src = $this->_fopen($path, 'rb')) == false) {
- return false;
- }
- if (($trg = fopen($tmb, 'wb')) == false) {
- $this->_fclose($src, $path);
- return false;
- }
- while (!feof($src)) {
- fwrite($trg, fread($src, 8192));
- }
- $this->_fclose($src, $path);
- fclose($trg);
- $result = false;
- $tmbSize = $this->tmbSize;
- $tmbHeight = $this->tmbHeight;
- if (($s = getimagesize($tmb)) == false) {
- return false;
- }
- /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
- if ($s[0] <= $tmbSize && $s[1] <= $tmbHeight) {
- $result = $this->imgResize($tmb, $s[0], $s[1], 'center', 'middle', $this->options['tmbBgColor']);
- } else {
- if ($this->options['tmbCrop']) {
- /* Resize and crop if image bigger than thumbnail */
- if (!(($s[0] > $tmbSize && $s[1] <= $tmbHeight) || ($s[0] <= $tmbSize && $s[1] > $tmbHeight) ) || ($s[0] > $tmbSize && $s[1] > $tmbHeight)) {
- $result = $this->imgResize($tmb, $tmbSize, $tmbHeight, true, false);
- }
- if (($s = getimagesize($tmb)) != false) {
- $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize)/2) : 0;
- $y = $s[1] > $tmbHeight ? intval(($s[1] - $tmbHeight)/2) : 0;
- $result = $this->imgCrop($tmb, $tmbSize, $tmbHeight, $x, $y);
- }
- } else {
- $result = $this->imgResize($tmb, $tmbSize, $tmbHeight, true, true, $this->imgLib);
- //$result = $this->imgSquareFit($tmb, $tmbSize, $tmbHeight, 'center', 'middle', $this->options['tmbBgColor'] );
- }
- }
- if (!$result) {
- unlink($tmb);
- return false;
- }
- return $name;
- }
- /**
- * Resize image
- *
- * @param string $path image file
- * @param int $width new width
- * @param int $height new height
- * @param bool $keepProportions crop image
- * @param bool $resizeByBiggerSide resize image based on bigger side if true
- * @param string $destformat image destination format
- * @return string|false
- * @author Dmitry (dio) Levashov
- * @author Alexey Sukhotin
- **/
- protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null) {
- if (($s = @getimagesize($path)) == false) {
- return false;
- }
- $result = false;
- list($size_w, $size_h) = array($width, $height);
- if ($keepProportions == true) {
- list($orig_w, $orig_h, $new_w, $new_h) = array($s[0], $s[1], $width, $height);
- /* Calculating image scale width and height */
- $xscale = $orig_w / $new_w;
- $yscale = $orig_h / $new_h;
- /* Resizing by biggest side */
- if ($resizeByBiggerSide) {
- if ($orig_w > $orig_h) {
- $size_h = $orig_h * $width / $orig_w;
- $size_w = $width;
- } else {
- $size_w = $orig_w * $height / $orig_h;
- $size_h = $height;
- }
- } else {
- if ($orig_w > $orig_h) {
- $size_w = $orig_w * $height / $orig_h;
- $size_h = $height;
- } else {
- $size_h = $orig_h * $width / $orig_w;
- $size_w = $width;
- }
- }
- }
- switch ($this->imgLib) {
- case 'imagick':
- try {
- $img = new imagick($path);
- } catch (Exception $e) {
- return false;
- }
- $img->resizeImage($size_w, $size_h, Imagick::FILTER_LANCZOS, true);
- $result = $img->writeImage($path);
- return $result ? $path : false;
- break;
- case 'gd':
- if ($s['mime'] == 'image/jpeg') {
- $img = imagecreatefromjpeg($path);
- } elseif ($s['mime'] == 'image/png') {
- $img = imagecreatefrompng($path);
- } elseif ($s['mime'] == 'image/gif') {
- $img = imagecreatefromgif($path);
- } elseif ($s['mime'] == 'image/xbm') {
- $img = imagecreatefromxbm($path);
- }
- if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
- if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
- return false;
- }
- if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
- $result = imagejpeg($tmp, $path, 100);
- } else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
- $result = imagegif($tmp, $path, 7);
- } else {
- $result = imagepng($tmp, $path, 7);
- }
- imagedestroy($img);
- imagedestroy($tmp);
- return $result ? $path : false;
- }
- break;
- }
- return false;
- }
- /**
- * Crop image
- *
- * @param string $path image file
- * @param int $width crop width
- * @param int $height crop height
- * @param bool $x crop left offset
- * @param bool $y crop top offset
- * @param string $destformat image destination format
- * @return string|false
- * @author Dmitry (dio) Levashov
- * @author Alexey Sukhotin
- **/
- protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
- if (($s = @getimagesize($path)) == false) {
- return false;
- }
- $result = false;
- switch ($this->imgLib) {
- case 'imagick':
- try {
- $img = new imagick($path);
- } catch (Exception $e) {
- return false;
- }
- $img->cropImage($width, $height, $x, $y);
- $result = $img->writeImage($path);
- return $result ? $path : false;
- break;
- case 'gd':
- if ($s['mime'] == 'image/jpeg') {
- $img = imagecreatefromjpeg($path);
- } elseif ($s['mime'] == 'image/png') {
- $img = imagecreatefrompng($path);
- } elseif ($s['mime'] == 'image/gif') {
- $img = imagecreatefromgif($path);
- } elseif ($s['mime'] == 'image/xbm') {
- $img = imagecreatefromxbm($path);
- }
- if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
- if (!imagecopy($tmp, $img, 0, 0, $x, $y, $width, $height)) {
- return false;
- }
- if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
- $result = imagejpeg($tmp, $path, 100);
- } else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
- $result = imagegif($tmp, $path, 7);
- } else {
- $result = imagepng($tmp, $path, 7);
- }
- imagedestroy($img);
- imagedestroy($tmp);
- return $result ? $path : false;
- }
- break;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement