function makeThumbnail($ImageFile, $CacheFolder = "", $ThumbSize = array("300", "200")) { global $hostConfig; @mkdir($CachePath); $ImageName = basename($ImageFile); $ThumbFile = "{$CacheFolder}/{$ImageName}"; header("Content-Type: image/jpeg"); // Use a cached copy of the Thumbnail, if it exists (and has a valid size) if (file_exists($ThumbFile)) { if (filesize($ThumbFile) >= 1500) { return readfile($ThumbFile); } unlink($ThumbFile); // If <1500bytes, assume this thumbnail is corrupt. Delete it from disk, to be re-generated by the code below.. } // Generate a new thumbnail $thumb = imagecreatetruecolor($ThumbSize[0], $ThumbSize[1]); fastimagecopyresampled($ImageFile, $thumb, $ThumbSize[0], $ThumbSize[1]); imagejpeg($thumb, "/tmp/{$ImageName}.tmp", 90); // Display image imagejpeg($thumb); // Copy into the cache ** NOTE: Using a shell exec, because imagejpeg() writes 0-byte files to the CIFS share under Docker (@ 12/09/15) echo `cp /tmp/{$ImageName}.tmp {$ThumbFile}`; exit(0); }
static function createThumb($name, $thumbname, $new_w, $new_h) { $system = explode('.', $name); $ext = $system[1]; if (preg_match('/jpg|jpeg|JPG/', $ext)) { $src_img = imagecreatefromjpeg($name); $img_type = "jpg"; } else { if (preg_match('/png/PNG', $ext)) { $src_img = imagecreatefrompng($name); $img_type = "png"; } } $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = $old_y * ($new_h / $old_x); } else { if ($old_x < $old_y) { $thumb_w = $old_x * ($new_w / $old_y); $thumb_h = $new_h; } else { $thumb_w = $new_w; $thumb_h = $new_h; } } $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); fastimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y, 3.25); if ($img_type == "png") { imagepng($dst_img, $thumbname); } else { imagejpeg($dst_img, $thumbname); } imagedestroy($dst_img); imagedestroy($src_img); }
function createthumb($name, $filename, $new_w, $new_h) { if (file_exists($name)) { if (!MakeDirectory(dirname($filename), 0777, true)) { echo "Error: " . $thumbsfolder . " Does not exist and could not be created.<BR>"; } } else { echo $name . " does not exist. Cannot generate thumbnails."; return; } $system = explode(".", $name); $src_img = ""; if (preg_match("/jpg|jpeg|JPG|JPEG/", $system[1])) { $src_img = imagecreatefromjpeg($name); } if (preg_match("/png|PNG/", $system[1])) { $src_img = imagecreatefrompng($name); } $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = $old_y * ($new_h / $old_x); } if ($old_x < $old_y) { $thumb_w = $old_x * ($new_w / $old_y); $thumb_h = $new_h; } if ($old_x == $old_y) { $thumb_w = $new_w; $thumb_h = $new_h; } $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); fastimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); /*imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); */ if (preg_match("/png/", $system[1])) { imagepng($dst_img, $filename); } else { imagejpeg($dst_img, $filename); } imagedestroy($dst_img); imagedestroy($src_img); }
private function createThumb($url, $filename, $type, $width, $height) { # Check dependencies self::dependencies(isset($this->database, $this->settings, $url, $filename, $type, $width, $height)); # Call plugins $this->plugins(__METHOD__, 0, func_get_args()); # Size of the thumbnail $newWidth = 200; $newHeight = 200; $photoName = explode('.', $filename); $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg'; $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg'; # Create thumbnails with Imagick if (extension_loaded('imagick') && $this->settings['imagick'] === '1') { # Read image $thumb = new Imagick(); $thumb->readImage($url); $thumb->setImageCompressionQuality($this->settings['thumbQuality']); $thumb->setImageFormat('jpeg'); # Copy image for 2nd thumb version $thumb2x = clone $thumb; # Create 1st version $thumb->cropThumbnailImage($newWidth, $newHeight); $thumb->writeImage($newUrl); $thumb->clear(); $thumb->destroy(); # Create 2nd version $thumb2x->cropThumbnailImage($newWidth * 2, $newHeight * 2); $thumb2x->writeImage($newUrl2x); $thumb2x->clear(); $thumb2x->destroy(); } else { # Create image $thumb = imagecreatetruecolor($newWidth, $newHeight); $thumb2x = imagecreatetruecolor($newWidth * 2, $newHeight * 2); # Set position if ($width < $height) { $newSize = $width; $startWidth = 0; $startHeight = $height / 2 - $width / 2; } else { $newSize = $height; $startWidth = $width / 2 - $height / 2; $startHeight = 0; } # Create new image switch ($type) { case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break; case 'image/png': $sourceImg = imagecreatefrompng($url); break; case 'image/gif': $sourceImg = imagecreatefromgif($url); break; default: Log::error($this->database, __METHOD__, __LINE__, 'Type of photo is not supported'); return false; break; } # Create thumb fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize); imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']); imagedestroy($thumb); # Create retina thumb fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth * 2, $newHeight * 2, $newSize, $newSize); imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']); imagedestroy($thumb2x); # Free memory imagedestroy($sourceImg); } # Call plugins $this->plugins(__METHOD__, 1, func_get_args()); return true; }
/** * Draw the appropriate thumbnail for the given frame * * @param mixed $test * @param mixed $frameTime * @param mixed $im */ function DrawTest(&$test, $frameTime, $im) { global $backgroundColor; $updated = false; // find the closest video frame <= the target time $frame_ms = null; foreach ($test['frames'] as $ms => $frame) { if ($ms <= $frameTime && $ms <= $test['end'] && (!isset($frame_ms) || $ms > $frame_ms)) { $frame_ms = $ms; } } $path = null; if (isset($frame_ms)) { $path = $test['frames'][$frame_ms]['path']; } $need_grey = false; if (!isset($test['done']) && $frameTime > $test['end']) { $need_grey = true; $test['done'] = true; } // see if we actually need to draw anything if (isset($path) && (!isset($test['lastFrame']) || $test['lastFrame'] !== $path || $need_grey)) { $test['lastFrame'] = $path; if (strtolower(substr($path, -4)) == '.png') { $thumb = imagecreatefrompng("./{$path}"); } else { $thumb = imagecreatefromjpeg("./{$path}"); } if ($thumb) { if ($need_grey) { imagefilter($thumb, IMG_FILTER_GRAYSCALE); } // Scale and center the thumbnail aspect-correct in the area reserved for it $rect = $test['thumbRect']; $thumb_w = imagesx($thumb); $thumb_h = imagesy($thumb); $scale = min($rect['width'] / $thumb_w, $rect['height'] / $thumb_h); $w = min(floor($thumb_w * $scale), $rect['width']); $h = min(floor($thumb_h * $scale), $rect['height']); $x = $rect['x'] + floor(($rect['width'] - $w) / 2); $y = $rect['y'] + floor(($rect['height'] - $h) / 2); imagefilledrectangle($im, $x, $y, $x + $w, $y + $h, $backgroundColor); fastimagecopyresampled($im, $thumb, $x, $y, 0, 0, $w, $h, $thumb_w, $thumb_h, 4); imagedestroy($thumb); $updated = true; } } if (isset($test['timeRect']) && $frameTime <= $test['end'] && DrawFrameTime($test, $frameTime, $im, $test['timeRect'])) { $updated = true; } return $updated; }
$cached = '_cached'; } $imgPath = GetTestPath($test['id']) . "/video_{$test['run']}{$cached}/{$path}"; if ($lastThumb != $path || !$thumb) { if ($lastThumb != $path) { $border = $colChanged; } // load the new thumbnail if ($thumb) { imagedestroy($thumb); unset($thuumb); } $tmp = imagecreatefromjpeg("./{$imgPath}"); if ($tmp) { $thumb = imagecreatetruecolor($test['video']['thumbWidth'], $test['video']['thumbHeight']); fastimagecopyresampled($thumb, $tmp, 0, 0, 0, 0, $test['video']['thumbWidth'], $test['video']['thumbHeight'], imagesx($tmp), imagesy($tmp), 4); imagedestroy($tmp); } } // draw the thumbnail $left += $colMargin; $width = imagesx($thumb); $padding = ($columnWidth - $width) / 2; if (isset($border)) { imagefilledrectangle($im, $left - 2 + $padding, $top - 2, $left + imagesx($thumb) + 2 + $padding, $top + imagesy($thumb) + 2, $border); } imagecopy($im, $thumb, $left + $padding, $top, 0, 0, $width, imagesy($thumb)); $left += $columnWidth + $colMargin; $lastThumb = $path; } }
/** * Calculate histograms for each color channel for the given image */ function GetImageHistogram($image_file, $options, $histograms) { $histogram = null; $ext = strripos($image_file, '.jpg'); if ($ext !== false) { $histogram_file = substr($image_file, 0, $ext) . '.hist'; } else { $ext = strripos($image_file, '.png'); if ($ext !== false) { $histogram_file = substr($image_file, 0, $ext) . '.hist'; } } if (isset($histograms)) { // figure out the timestamp for the video frame in ms $ms = null; if (preg_match('/ms_(?P<ms>[0-9]+)\\.(png|jpg)/i', $image_file, $matches)) { $ms = intval($matches['ms']); } elseif (preg_match('/frame_(?P<ms>[0-9]+)\\.(png|jpg)/i', $image_file, $matches)) { $ms = intval($matches['ms']) * 100; } foreach ($histograms as &$hist) { if (isset($hist['histogram']) && isset($hist['time']) && $hist['time'] == $ms) { $histogram = $hist['histogram']; break; } } } // See if we have the old-style histograms (separate files) if (!isset($histogram) && !isset($options) && isset($histogram_file) && is_file($histogram_file)) { $histogram = json_decode(file_get_contents($histogram_file), true); if (!is_array($histogram) || !array_key_exists('r', $histogram) || !array_key_exists('g', $histogram) || !array_key_exists('b', $histogram) || count($histogram['r']) != 256 || count($histogram['g']) != 256 || count($histogram['b']) != 256) { unset($histogram); } } // generate a histogram from the image itself if (!isset($histogram)) { $im = imagecreatefromjpeg($image_file); if ($im !== false) { $width = imagesx($im); $height = imagesy($im); if ($width > 0 && $height > 0) { // default a resample to 1/4 in each direction which will significantly speed up processing with minimal impact to accuracy. // This is only for calculations done on the server. Histograms from the client look at every pixel $resample = 8; if (isset($options) && array_key_exists('resample', $options)) { $resample = $options['resample']; } if ($resample > 2) { $oldWidth = $width; $oldHeight = $height; $width = intval($width * 2 / $resample); $height = intval($height * 2 / $resample); $tmp = imagecreatetruecolor($width, $height); fastimagecopyresampled($tmp, $im, 0, 0, 0, 0, $width, $height, $oldWidth, $oldHeight, 3); imagedestroy($im); $im = $tmp; unset($tmp); } $histogram = array(); $histogram['r'] = array(); $histogram['g'] = array(); $histogram['b'] = array(); $buckets = 256; if (isset($options) && array_key_exists('buckets', $options) && $options['buckets'] >= 1 && $options['buckets'] <= 256) { $buckets = $options['buckets']; } for ($i = 0; $i < $buckets; $i++) { $histogram['r'][$i] = 0; $histogram['g'][$i] = 0; $histogram['b'][$i] = 0; } for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { $rgb = ImageColorAt($im, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; // ignore white pixels if ($r != 255 || $g != 255 || $b != 255) { if (isset($options) && array_key_exists('colorSpace', $options) && $options['colorSpace'] != 'RGB') { if ($options['colorSpace'] == 'HSV') { RGB_TO_HSV($r, $g, $b); } elseif ($options['colorSpace'] == 'YUV') { RGB_TO_YUV($r, $g, $b); } } $bucket = (int) (($r + 1.0) / 256.0 * $buckets) - 1; $histogram['r'][$bucket]++; $bucket = (int) (($g + 1.0) / 256.0 * $buckets) - 1; $histogram['g'][$bucket]++; $bucket = (int) (($b + 1.0) / 256.0 * $buckets) - 1; $histogram['b'][$bucket]++; } } } } imagedestroy($im); unset($im); } if (!isset($options) && isset($histogram_file) && !is_file($histogram_file) && isset($histogram)) { file_put_contents($histogram_file, json_encode($histogram)); } } return $histogram; }
function createThumbnail($file_location, $thumb_location, $new_w, $new_h) { if (TINYIB_THUMBNAIL == 'gd') { $system = explode(".", $thumb_location); $system = array_reverse($system); if (preg_match("/jpg|jpeg/", $system[0])) { $src_img = imagecreatefromjpeg($file_location); } else { if (preg_match("/png/", $system[0])) { $src_img = imagecreatefrompng($file_location); } else { if (preg_match("/gif/", $system[0])) { $src_img = imagecreatefromgif($file_location); } else { return false; } } } if (!$src_img) { fancyDie("Unable to read uploaded file during thumbnailing. A common cause for this is an incorrect extension when the file is actually of a different type."); } $old_x = imageSX($src_img); $old_y = imageSY($src_img); $percent = $old_x > $old_y ? $new_w / $old_x : $new_h / $old_y; $thumb_w = round($old_x * $percent); $thumb_h = round($old_y * $percent); $dst_img = imagecreatetruecolor($thumb_w, $thumb_h); if (preg_match("/png/", $system[0]) && imagepng($src_img, $thumb_location)) { imagealphablending($dst_img, false); imagesavealpha($dst_img, true); $color = imagecolorallocatealpha($dst_img, 0, 0, 0, 0); imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $color); imagecolortransparent($dst_img, $color); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); } else { fastimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); } if (preg_match("/png/", $system[0])) { if (!imagepng($dst_img, $thumb_location)) { return false; } } else { if (preg_match("/jpg|jpeg/", $system[0])) { if (!imagejpeg($dst_img, $thumb_location, 70)) { return false; } } else { if (preg_match("/gif/", $system[0])) { if (!imagegif($dst_img, $thumb_location)) { return false; } } } } imagedestroy($dst_img); imagedestroy($src_img); } else { // imagemagick $discard = ''; $exit_status = 1; exec("convert {$file_location} -auto-orient -thumbnail '" . $new_w . "x" . $new_h . "' -coalesce -layers OptimizeFrame -depth 4 -type palettealpha {$thumb_location}", $discard, $exit_status); if ($exit_status != 0) { return false; } } return true; }
function directResize($img_src, $img_min_path, $img_min_prefix, $img_min_w, $img_min_h, $img_min_r, $img_min_q_jpg, $img_min_q_png) { // Calculate image values $img_src_ext = substr(strrchr($img_src, '.'), 1); // extract image extension $img_src_name = basename($img_src, "." . $img_src_ext); // original image name // image width and height values if ($img_min_w == "") { $img_min_w = 0; } if ($img_min_h == "") { $img_min_h = 0; } if ($img_min_w == 0) { $img_min_r = 2; } if ($img_min_h == 0) { $img_min_r = 1; } // check that the image file exists $verif = true; if (!file_exists($img_src)) { $verif = false; } else { // pull out the dimensions of the original image $size = getimagesize($img_src); $img_src_w = $size[0]; $img_src_h = $size[1]; } // check the file extension is one that can be handled, jpg,jpeg,gif and png $img_ext = strtolower($img_src_ext); if ($img_ext != "jpg" && $img_ext != "jpeg" && $img_ext != "gif" && $img_ext != "png") { $verif = false; } // check the output folder for the images exist if (!is_dir($img_min_path)) { $verif = false; } // check that at least one value exists for either width or height if ($img_min_w == 0 && $img_min_h == 0) { $verif = false; } // check that we are changing at least one dimension if ($img_min_w == $img_src_w && $img_min_h == $img_src_h) { $verif = false; } // if everything checks out then proceed if ($verif) { // calculate the target dimensions switch ($img_min_r) { case 0: $img_min_w_calc = $img_min_w; $img_min_h_calc = $img_min_h; break; case 1: $img_min_w_calc = $img_min_w; $img_min_h_calc = round($img_src_h * $img_min_w_calc / $img_src_w); break; case 2: $img_min_h_calc = $img_min_h; $img_min_w_calc = round($img_src_w * $img_min_h_calc / $img_src_h); break; case 3: $ratio_wh = $img_src_w / $img_src_h; $ratio_whmin = $img_min_w / $img_min_h; if ($ratio_wh > $ratio_whmin) { $img_min_w_calc = $img_min_w; $img_min_h_calc = round($img_src_h * $img_min_w_calc / $img_src_w); } else { $img_min_h_calc = $img_min_h; $img_min_w_calc = round($img_src_w * $img_min_h_calc / $img_src_h); } break; case 4: if ($img_src_w / $img_src_h > $img_min_w / $img_min_h) { $img_min_h_calc = $img_min_h; $img_min_w_calc = round($img_src_w * $img_min_h_calc / $img_src_h); } else { $img_min_w_calc = $img_min_w; $img_min_h_calc = round($img_src_h * $img_min_w_calc / $img_src_w); } break; } // calculate filename for thumbnail $img_min_name = $img_min_prefix . $img_src_name . "_w" . $img_min_w_calc . "_h" . $img_min_h_calc . "." . $img_src_ext; // sort out target folder for thumbnail if (substr($img_min_path, -1) == "/") { $img_min_path_name = $img_min_path . $img_min_name; } else { $img_min_path_name = $img_min_path . "/" . $img_min_name; } // if the thumbnail doesn't exist then create it. if (!file_exists($img_min_path_name)) { // increase the memory limit ini_set('memory_limit', '64M'); // create thumbnail according to the file extension if ($img_ext == "jpg" || $img_ext == "jpeg") { $image_p = imagecreatetruecolor($img_min_w_calc, $img_min_h_calc); $image = imagecreatefromjpeg($img_src); fastimagecopyresampled($image_p, $image, 0, 0, 0, 0, $img_min_w_calc, $img_min_h_calc, $img_src_w, $img_src_h, 4); imagejpeg($image_p, $img_min_path_name, $img_min_q_jpg); } else { if ($img_ext == "gif") { $image_p = imagecreatetruecolor($img_min_w_calc, $img_min_h_calc); $image = imagecreatefromgif($img_src); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $img_min_w_calc, $img_min_h_calc, $img_src_w, $img_src_h); imagegif($image_p, $img_min_path_name); } else { if ($img_ext == "png") { $image_p = imagecreatetruecolor($img_min_w_calc, $img_min_h_calc); $image = imagecreatefrompng($img_src); // not all php installations seem to have imageantialias in the GD package eg Ubuntu - use if available if (var_dump(function_exists('imageantialias'))) { imageantialias($image_p, true); } //... define antialiasing mode imagealphablending($image_p, false); //... disable blending mode on transparent image imagesavealpha($image_p, true); //... set full information for the alpha channel $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 0); //... allocate a colour to the alpha channel for ($x = 0; $x < $img_min_w_calc; $x++) { for ($y = 0; $y < $img_min_h_calc; $y++) { imagesetpixel($image_p, $x, $y, $transparent); //... draws a pixel at the specified coordinates } } imagecopyresampled($image_p, $image, 0, 0, 0, 0, $img_min_w_calc, $img_min_h_calc, $img_src_w, $img_src_h); imagepng($image_p, $img_min_path_name, $img_min_q_png); } } } // delete the temporary files ImageDestroy($image_p); ImageDestroy($image); } $pathFinal = $img_min_path_name; } else { $pathFinal = $img_src; } return $pathFinal; }
function save_thumbnail($image, $size, $save_path, $save_name, $config = array()) { $myreturn = false; $size = array($size, $size); if (empty($config['padding_type'])) { $config['padding_type'] = PAD_1SIDE; } if (empty($config['quality'])) { $config['quality'] = 90; } if ($imginfo = getimagesize($image)) { $orig_size = array($imginfo[0], $imginfo[1]); if ($orig_size[0] / $size[0] < $orig_size[1] / $size[1]) { $relevant_length = 1; } else { $relevant_length = 0; } if ($imginfo[2] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) { //gif $myimg = @imagecreatefromgif($image); } elseif ($imginfo[2] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) { //jpg ob_start(); $myimg = @imagecreatefromjpeg($image); ob_end_flush(); } elseif ($imginfo[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')) { //png $myimg = @imagecreatefrompng($image); } if (!empty($myimg)) { $new_size = array(); $mynewimg = ''; if ($orig_size[$relevant_length] > $size[$relevant_length]) { // scale down $new_size[$relevant_length] = $size[$relevant_length]; $new_size[1 - $relevant_length] = (int) ($orig_size[1 - $relevant_length] * ($size[$relevant_length] / $orig_size[$relevant_length])); if ($config['padding_type'] == PAD_1SIDE || $config['padding_type'] == PAD_2SIDES) { // $size=$size; // this is actually just PAD_1SIDE and the photo will be square } else { $size = $new_size; // no padding here, photo has original proportions } } else { // picture is smaller than the needed size $new_size = $orig_size; if ($config['padding_type'] == PAD_2SIDES) { //pad in both directions. square and big // $size=array($size,$size); } elseif ($config['padding_type'] == PAD_1SIDE) { // padding in one direction only. square but smaller $size = $orig_size[$relevant_length]; $size = array($size, $size); } else { // no padding. original proportions $size = $orig_size; } } $mynewimg = @imagecreatetruecolor($size[0], $size[1]); imagefilledrectangle($mynewimg, 0, 0, $size[0], $size[1], 0xffffff); $x = (int) (($size[0] - $new_size[0]) / 2); $y = (int) (($size[1] - $new_size[1]) / 2); if (defined('BICUBIC_RESAMPLE')) { imagecopyresamplebicubic($mynewimg, $myimg, $x, $y, 0, 0, $new_size[0], $new_size[1], $orig_size[0], $orig_size[1]); } else { fastimagecopyresampled($mynewimg, $myimg, $x, $y, 0, 0, $new_size[0], $new_size[1], $orig_size[0], $orig_size[1]); } if (!empty($config['watermark_text']) && function_exists('imagettftext')) { $config['watermark_text_color'] = str_pad($config['watermark_text_color'], 6, '0', STR_PAD_RIGHT); $text_color = imagecolorallocate($mynewimg, hexdec(substr($config['watermark_text_color'], 0, 2)), hexdec(substr($config['watermark_text_color'], 2, 2)), hexdec(substr($config['watermark_text_color'], 4, 2))); $text_color2 = imagecolorallocate($mynewimg, 255 - hexdec(substr($config['watermark_text_color'], 0, 2)), 255 - hexdec(substr($config['watermark_text_color'], 2, 2)), 255 - hexdec(substr($config['watermark_text_color'], 4, 2))); $font_size = 15; do { --$font_size; $text_box = imagettfbbox($font_size, 0, _BASEPATH_ . '/includes/fonts/arial.ttf', $config['watermark_text']); $textlen = $text_box[2] - $text_box[0] + 5; } while ($textlen > $new_size[0]); $watermark_x = (int) (($size[0] - $new_size[0]) / 2) + 5; $watermark_y = $new_size[1] + (int) (($size[1] - $new_size[1]) / 2) - 20; //shadow first imagettftext($mynewimg, $font_size, 0, $watermark_x, $watermark_y, $text_color2, _BASEPATH_ . '/includes/fonts/arial.ttf', $config['watermark_text']); //text second imagettftext($mynewimg, $font_size, 0, $watermark_x + 1, $watermark_y + 1, $text_color, _BASEPATH_ . '/includes/fonts/arial.ttf', $config['watermark_text']); } if (!empty($config['watermark_image']) && is_file($config['watermark_image'])) { $wm_image = @imagecreatefrompng($config['watermark_image']); $wm_image_width = imagesx($wm_image); $wm_image_height = imagesy($wm_image); $wm_image_x = (int) (($size[0] - $new_size[0]) / 2) + 5; $wm_image_y = $new_size[1] + (int) (($size[1] - $new_size[1]) / 2) - $wm_image_height; if (defined('BICUBIC_RESAMPLE')) { imagecopyresamplebicubic($mynewimg, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_width, $wm_image_height, $wm_image_width, $wm_image_height); } else { fastimagecopyresampled($mynewimg, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_width, $wm_image_height, $wm_image_width, $wm_image_height); } } if (!empty($config['round_corners'])) { $skin = get_my_skin(); imagealphablending($mynewimg, true); // put the corners $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_tl.png'); imagecopy($mynewimg, $corner, 0, 0, 0, 0, 7, 7); $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_tr.png'); imagecopy($mynewimg, $corner, $size[0] - 7, 0, 0, 0, 7, 7); $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_bl.png'); imagecopy($mynewimg, $corner, 0, $size[1] - 7, 0, 0, 7, 7); $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_br.png'); imagecopy($mynewimg, $corner, $size[0] - 7, $size[1] - 7, 0, 0, 7, 7); // draw the border lines $border_color = imagecolorallocate($mynewimg, 0xcc, 0xcc, 0xcc); imageline($mynewimg, 7, 0, $size[0] - 8, 0, $border_color); //tl->tr imageline($mynewimg, $size[0] - 1, 7, $size[0] - 1, $size[1] - 8, $border_color); //tr->br imageline($mynewimg, 7, $size[1] - 1, $size[0] - 8, $size[1] - 1, $border_color); //bl->br imageline($mynewimg, 0, 7, 0, $size[1] - 8, $border_color); //tl->bl } $myreturn = imagejpeg($mynewimg, $save_path . '/' . $save_name . '.jpg', $config['quality']); } else { $myreturn = false; } } else { $myreturn = false; } if (!empty($myimg)) { imagedestroy($myimg); } if (!empty($mynewimg)) { imagedestroy($mynewimg); } return $myreturn; }
/** * Resize the image down to thumbnail size * * @param mixed $img */ function GenerateThumbnail(&$img, $type) { global $newWidth; global $fit; // figure out what the height needs to be $width = imagesx($img); $height = imagesy($img); if ($fit > 0) { if ($width > $height) { $scale = $fit / $width; } else { $scale = $fit / $height; } } else { $scale = $newWidth / $width; } if ($scale < 1) { $newWidth = (int) ($width * $scale); $newHeight = (int) ($height * $scale); # Create a new temporary image $tmp = imagecreatetruecolor($newWidth, $newHeight); # Copy and resize old image into new image $quality = 4; if (!strcasecmp($type, 'jpg')) { $quality = 3; } fastimagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height, $quality); imagedestroy($img); $img = $tmp; unset($tmp); } }
private function createThumb($url, $filename, $type, $width, $height) { # Function that uses avconv to generate a thumbnail for a video file # Expects the following: # (string) $url : the path to the original video file # (string) $filename : the filename without path # (string) $type : dunno why this is needed right now, only mp4 is supported # (int) $width # (int) $height # Returns the following: # (bool) $return : true on success, false otherwise # Check dependencies self::dependencies(isset($this->database, $url, $filename, $this->settings, $type, $width, $height)); # Call Plugins $this->plugins(__METHOD__, 0, func_get_args()); #First step is to take a frame from the video which will then be resized $videoName = explode('.', $filename); $thumbOriginalName = $videoName[0] . "@original.jpg"; $thumbOriginalPath = LYCHEE_UPLOADS_THUMB . $thumbOriginalName; $command = "avconv -itsoffset -4 -i " . $url . " -vcodec mjpeg -vframes 1 -an -f rawvideo -s " . $width . "x" . $height . " " . $thumbOriginalPath; Log::notice($this->database, __METHOD__, __LINE__, "Command: " . $command); exec($command); # Next create the actual thumbnails using the same code as used for photos # Size of the thumbnail $newWidth = 200; $newHeight = 200; $iconWidth = 50; $iconHeight = 50; $videoName = explode('.', $filename); $newUrl = LYCHEE_UPLOADS_THUMB . $videoName[0] . '.jpeg'; $newUrl2x = LYCHEE_UPLOADS_THUMB . $videoName[0] . '@2x.jpeg'; # Create thumbnails with Imagick if (extension_loaded('imagick') && $this->settings['imagick'] === '1') { # Read icon image first $icon = new Imagick(LYCHEE . "/src/images/icon_play_overlay.png"); # Read image $thumb = new Imagick(); $thumb->readImage($thumbOriginalPath); $thumb->setImageCompressionQuality($this->settings['thumbQuality']); $thumb->setImageFormat('jpeg'); #Set the colorspace of the icon to the same as the image $icon->setImageColorspace($thumb->getImageColorspace()); # Copy image for 2nd thumb version $thumb2x = clone $thumb; $icon2x = clone $icon; # Create 1st version $thumb->cropThumbnailImage($newWidth, $newHeight); #Composite the icon $icon->cropThumbnailImage($iconWidth, $iconHeight); $thumb->compositeImage($icon, imagick::COMPOSITE_DEFAULT, $newWidth / 2 - $iconWidth / 2, $newHeight / 2 - $iconHeight / 2); #Save the small thumbnail $thumb->writeImage($newUrl); $thumb->clear(); $thumb->destroy(); # Create 2nd version $thumb2x->cropThumbnailImage($newWidth * 2, $newHeight * 2); # Composite the icon $icon2x->cropThumbnailImage($iconWidth * 2, $iconHeight * 2); $thumb2x->compositeImage($icon2x, imagick::COMPOSITE_DEFAULT, $newWidth - $iconWidth, $newHeight - $iconHeight); $thumb2x->writeImage($newUrl2x); $thumb2x->clear(); $thumb2x->destroy(); } else { # Read icon image first $iconPath = LYCHEE . "/src/images/icon_play_overlay.png"; $iconSize = getimagesize($iconPath); $icon = imagecreatetruecolor($iconSize[0], $iconSize[1]); # Create image $thumb = imagecreatetruecolor($newWidth, $newHeight); $thumb2x = imagecreatetruecolor($newWidth * 2, $newHeight * 2); # Set position if ($width < $height) { $newSize = $width; $startWidth = 0; $startHeight = $height / 2 - $width / 2; } else { $newSize = $height; $startWidth = $width / 2 - $height / 2; $startHeight = 0; } # Create new image $sourceImg = imagecreatefromjpeg($thumbOriginalPath); $sourceIcon = imagecreatefrompng($iconPath); # Create thumb fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize); fastimagecopyresampled($thumb, $sourceIcon, $newWidth / 2 - $iconWidth / 2, $newHeight / 2 - $iconHeight / 2, 0, 0, $iconWidth, $iconHeight, $iconSize[0], $iconSize[1]); imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']); imagedestroy($thumb); # Create retina thumb fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth * 2, $newHeight * 2, $newSize, $newSize); fastimagecopyresampled($thumb2x, $sourceIcon, $newWidth - $iconWidth, $newHeight - $iconHeight, 0, 0, $iconWidth * 2, $iconHeight * 2, $iconSize[0], $iconSize[1]); imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']); imagedestroy($thumb2x); # Free memory imagedestroy($sourceImg); imagedestroy($sourceIcon); } # Finally delete the original thumbnail frame unlink($thumbOriginalPath); return true; }
function resize($original, $w, $h, $crop, $cropratio = 0, $wm = 0, $catid = '') { require dirname(__FILE__) . DS . 'config.datsogallery.php'; jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); $cro = str_replace(':', 'x', $cropratio); $dirname = "datsogallery_catid-{$catid}_{$w}x{$h}_{$cro}"; $types = array(1 => 'gif', 'jpeg', 'png'); if (!JFolder::exists(JPATH_SITE . DS . 'cache' . DS . $dirname)) { JFolder::create(JPATH_SITE . DS . 'cache' . DS . $dirname); $content = '<html><body bgcolor="#ffffff"></body></html>'; JFile::write(JPATH_SITE . DS . 'cache' . DS . $dirname . DS . 'index.html', $content); } if (!JFile::exists(JPATH_SITE . $ad_pathoriginals . DS . 'blank.jpg')) { JFile::copy(JPATH_COMPONENT_SITE . DS . 'images' . DS . 'blank.jpg', JPATH_SITE . $ad_pathoriginals . DS . 'blank.jpg'); } $path = JPath::clean(JPATH_SITE . $ad_pathoriginals . DS . $original); dgFileCheck($path); if (!file_exists($path) || is_dir($path) || !($size = getimagesize($path))) { return; } $width = $size[0]; $height = $size[1]; $mw = $w; $mh = $h; $x = 0; $y = 0; if ($crop == '1') { $cr = explode(':', $cropratio); if (count($cr) == 2) { $rc = $width / $height; $crc = (double) $cr[0] / (double) $cr[1]; if ($rc < $crc) { $oh = $height; $height = $width / $crc; $y = ($oh - $height) / 2; } else { if ($rc > $crc) { $ow = $width; $width = $height * $crc; $x = ($ow - $width) / 2; } } } } $xr = $mw / $width; $yr = $mh / $height; if ($xr * $height < $mh) { $th = ceil($xr * $height); $tw = $mw; } else { $tw = ceil($yr * $width); $th = $mh; } $relfile = JURI::root(true) . '/cache/' . $dirname . '/' . basename($original); $cachefile = JPATH_SITE . DS . 'cache' . DS . $dirname . DS . basename($original); if (file_exists($cachefile)) { $cachesize = getimagesize($cachefile); $cached = $cachesize[0] == $tw && $cachesize[1] == $th; if (filemtime($cachefile) < filemtime($path)) { $cached = false; } } else { $cached = false; } if (!$cached && ($size[0] >= $w || $size[1] >= $h)) { $resize = $size[0] >= $w || $size[1] >= $h; } elseif (!$cached && ($size[0] <= $w || $size[1] <= $h)) { $resize = true; } else { $resize = false; } if ($resize) { @increasememory($original); $image = call_user_func('imagecreatefrom' . $types[$size[2]], $path); $temp = $size[0] <= $w || $size[1] <= $h ? imagecreatetruecolor($width, $height) : imagecreatetruecolor($tw, $th); if (function_exists('imagecreatetruecolor') && $temp) { if (in_array($types[$size[2]], array('gif', 'png'))) { $color = 'F5F5F5'; $background = imagecolorallocate($temp, hexdec($color[0] . $color[1]), hexdec($color[2] . $color[3]), hexdec($color[4] . $color[5])); imagefillalpha($temp, $background); } if ($resize && ($size[0] <= $w || $size[1] <= $h)) { if (in_array($types[$size[2]], array('gif', 'png'))) { imagecopyresampled($temp, $image, 0, 0, 0, 0, $size[0], $size[1], $width, $height); } else { fastimagecopyresampled($temp, $image, 0, 0, 0, 0, $size[0], $size[1], $width, $height); } } else { if (in_array($types[$size[2]], array('gif', 'png'))) { imagecopyresampled($temp, $image, 0, 0, $x, $y, $tw, $th, $width, $height); } else { fastimagecopyresampled($temp, $image, 0, 0, $x, $y, $tw, $th, $width, $height); } } imagedestroy($image); $sharpness = findsharp($width, $tw); $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1)); $divisor = $sharpness; $offset = 0; imageconvolution($temp, $sharpenMatrix, $divisor, $offset); if ($wm) { $watermarkPNGFile = JPATH_SITE . DS . 'components' . DS . 'com_datsogallery' . DS . 'images' . DS . 'watermark.png'; $watermarkMargin = 5; $watermark = imagecreatefrompng($watermarkPNGFile); $watermarkWidth = imagesx($watermark); $watermarkHeight = imagesy($watermark); $imageWidth = imagesx($temp); $imageHeight = imagesy($temp); switch ($ad_wmpos) { case 'topleft': $placeWatermarkX = $watermarkMargin; $placeWatermarkY = $watermarkMargin; break; case 'topright': $placeWatermarkX = $imageWidth - $watermarkWidth - $watermarkMargin; $placeWatermarkY = $watermarkMargin; break; case 'bottomleft': $placeWatermarkX = $watermarkMargin; $placeWatermarkY = $imageHeight - $watermarkHeight - $watermarkMargin; break; case 'bottomright': $placeWatermarkX = $imageWidth - $watermarkWidth - $watermarkMargin; $placeWatermarkY = $imageHeight - $watermarkHeight - $watermarkMargin; break; case 'center': $placeWatermarkX = ($imageWidth - $watermarkWidth) / 2 - $watermarkMargin; $placeWatermarkY = ($imageHeight - $watermarkHeight) / 2 - $watermarkMargin; break; } imagecopymerge_alpha($temp, $watermark, $placeWatermarkX, $placeWatermarkY, 0, 0, $watermarkWidth, $watermarkHeight, 0); } } if ($types[$size[2]] == 'jpeg') { dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname, 0777); call_user_func('image' . $types[$size[2]], $temp, $cachefile, $ad_thumbquality); dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname); } else { dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname, 0777); call_user_func('image' . $types[$size[2]], $temp, $cachefile); dgChmod(JPATH_SITE . DS . 'cache' . DS . $dirname); } imagedestroy($temp); } return $relfile; exit; }
/** * @ignore */ function createThumbnail($name, $filename, $new_w, $new_h) { $system = explode(".", $filename); $system = array_reverse($system); if (preg_match("/jpg|jpeg/", $system[0])) { $src_img = imagecreatefromjpeg($name); } else { if (preg_match("/png/", $system[0])) { $src_img = imagecreatefrompng($name); } else { if (preg_match("/gif/", $system[0])) { $src_img = imagecreatefromgif($name); } else { return false; } } } if (!$src_img) { echo '<br />Unable to open the uploaded image for thumbnailing. Maybe its a different filetype, and has the wrong extension?'; return false; } $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $percent = $new_w / $old_x; } else { $percent = $new_h / $old_y; } $thumb_w = round($old_x * $percent); $thumb_h = round($old_y * $percent); $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); fastimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); if (preg_match("/png/", $system[0])) { if (!imagepng($dst_img, $filename)) { echo 'unable to imagepng.'; return false; } } else { if (preg_match("/jpg|jpeg/", $system[0])) { if (!imagejpeg($dst_img, $filename, 70)) { echo 'unable to imagejpg.'; return false; } } else { if (preg_match("/gif/", $system[0])) { if (!imagegif($dst_img, $filename)) { echo 'unable to imagegif.'; return false; } } } } imagedestroy($dst_img); imagedestroy($src_img); return true; }