/** * Create image thumbnail * * @param string $img image file * @param string $tmb thumbnail name * @return bool **/ protected function _tmb($img, $tmb) { if (false == ($s = getimagesize($img))) { return false; } $tmbSize = $this->_options['tmbSize']; if ($this->_options['tmbCrop'] == false) { /* Calculating image scale width and height */ $xscale = $s[0] / $tmbSize; $yscale = $s[1] / $tmbSize; if ($yscale > $xscale) { $newwidth = round($s[0] * (1 / $yscale)); $newheight = round($s[1] * (1 / $yscale)); } else { $newwidth = round($s[0] * (1 / $xscale)); $newheight = round($s[1] * (1 / $xscale)); } /* Keeping original dimensions if image fitting into thumbnail without scale */ if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) { $newwidth = $s[0]; $newheight = $s[1]; } /* Calculating coordinates for aligning thumbnail */ $align_y = ceil(($tmbSize - $newheight) / 2); $align_x = ceil(($tmbSize - $newwidth) / 2); } switch ($this->_options['imgLib']) { case 'imagick': try { $_img = new imagick($img); } catch (Exception $e) { return false; } $_img->contrastImage(1); if ($this->_options['tmbCrop'] == false) { $img1 = new Imagick(); $img1->newImage($tmbSize, $tmbSize, new ImagickPixel($this->_options['tmbBgColor'])); $img1->setImageFormat('png'); $_img->resizeImage($newwidth, $newheight, NULL, true); $img1->compositeImage($_img, imagick::COMPOSITE_OVER, $align_x, $align_y); return $img1->writeImage($tmb); } else { return $_img->cropThumbnailImage($tmbSize, $tmbSize) && $_img->writeImage($tmb); } break; case 'mogrify': if (@copy($img, $tmb)) { list($x, $y, $size) = $this->_cropPos($s[0], $s[1]); // exec('mogrify -crop '.$size.'x'.$size.'+'.$x.'+'.$y.' -scale '.$tmbSize.'x'.$tmbSize.'! '.escapeshellarg($tmb), $o, $c); $mogrifyArgs = 'mogrify -resize ' . $tmbSize . 'x' . $tmbSize; if ($this->_options['tmbCrop'] == false) { $mogrifyArgs .= ' -gravity center -background "' . $this->_options['tmbBgColor'] . '" -extent ' . $tmbSize . 'x' . $tmbSize; } if ($this->_options['tmbCrop'] == false) { $mogrifyArgs .= ' ' . escapeshellarg($tmb); } exec($mogrifyArgs, $o, $c); if (file_exists($tmb)) { return true; } elseif ($c == 0) { // find tmb for psd and animated gif $mime = $this->_mimetype($img); if ($mime == 'image/vnd.adobe.photoshop' || ($mime = 'image/gif')) { $pinfo = pathinfo($tmb); $test = $pinfo['dirname'] . DIRECTORY_SEPARATOR . $pinfo['filename'] . '-0.' . $pinfo['extension']; if (file_exists($test)) { return rename($test, $tmb); } } } } break; case 'gd': /*if ($s['mime'] == 'image/jpeg') { $_img = imagecreatefromjpeg($img); } elseif ($s['mime'] == 'image/png') { $_img = imagecreatefrompng($img); } elseif ($s['mime'] == 'image/gif') { $_img = imagecreatefromgif($img); } if (!$_img || false == ($_tmb = imagecreatetruecolor($tmbSize, $tmbSize))) { return false; } if ($this->_options['tmbCrop'] == false) { list($r,$g,$b) = sscanf($this->_options['tmbBgColor'], "#%02x%02x%02x"); imagefill($_tmb, 0, 0, imagecolorallocate($_tmb, $r, $g, $b)); if (!imagecopyresampled($_tmb, $_img, $align_x, $align_y, 0, 0, $newwidth, $newheight, $s[0], $s[1])) { return false; } } else { list($x, $y, $size) = $this->_cropPos($s[0], $s[1]); if (!imagecopyresampled($_tmb, $_img, 0, 0, $x, $y, $tmbSize, $tmbSize, $size, $size)) { return false; } } $r = imagepng($_tmb, $tmb, 7); imagedestroy($_img); imagedestroy($_tmb); return $r;*/ $img = new JO_Thumb($img); if ($this->_options['tmbCrop'] == false) { $img->resize_crop($tmbSize, $tmbSize); } else { $img->resize($tmbSize, $tmbSize); } return $img->save($tmb); break; } }
public function resize($filename, $width, $height, $crop = false, $watermark = false, $gray = false, $auto = false) { if (!$width && !$height) { $width = 1; $height = 1; } if (!file_exists($this->dirImages . $filename) || !is_file($this->dirImages . $filename)) { $filename = JO_Registry::forceGet('no_image'); if (!$filename || !file_exists($this->dirImages . $filename) || !is_file($this->dirImages . $filename)) { $filename = 'no_image.jpg'; if (!file_exists($this->dirImages . $filename) || !is_file($this->dirImages . $filename)) { return; } } } $info = pathinfo($filename); $extension = $info['extension']; $gray_name = ''; if ($gray) { $gray_name = '-gray'; } if ($crop) { $gray_name .= '-crop'; } if ($watermark) { $gray_name .= '-watermark'; } if ($auto == 'width') { $whe = $width . 'x'; } elseif ($auto == 'height') { $whe = 'x' . $height; } else { $whe = $width . 'x' . $height; } $old_image = $filename; $tmp = substr($filename, 0, strrpos($filename, '.')); $filename = substr($filename, 0, strrpos($filename, '.')); // $filename = substr($tmp, 0, strrpos($tmp, '/')) . '/' . md5(basename($tmp)) . '-' . md5($filename); $new_image = 'cache' . $filename . '-' . $whe . $gray_name . '.' . $extension; $new_image = str_replace('/../', '/', $new_image); $this->temp_sizes = array('width' => $width, 'height' => $height); if (!file_exists($this->dirImages . $new_image) || filemtime($this->dirImages . $old_image) > filemtime($this->dirImages . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists($this->dirImages . $path)) { @mkdir($this->dirImages . $path, 0777, true); } } $image = new JO_Thumb($this->dirImages . $old_image); if ($crop === false) { $image->resize($width, $height); } else { $image->resize_crop($width, $height); } if ($watermark && JO_Registry::get($watermark) && file_exists(BASE_PATH . '/uploads/' . JO_Registry::get($watermark))) { $image->watermark(BASE_PATH . '/uploads/' . JO_Registry::get($watermark), false); } $image->save($this->dirImages . $new_image, $gray); } if (file_exists($this->dirImages . $new_image)) { $this->temp_sizes['date_added'] = filemtime($this->dirImages . $new_image); } return $this->request->getBaseUrl() . $this->httpImages . $new_image; }