function send_thumb($image, $compression, $sizex, $sizey, $generateOnly = false) { global $bg_color_preview_R, $bg_color_preview_G, $bg_color_preview_B; set_error_handler("on_error_no_output"); ini_set("gd.jpeg_ignore_warning", 1); // since php 5.1.3 this leads that corrupt jpgs are read much better! set_error_handler("on_error"); $srcx = 0; $srcy = 0; $dimx = $sizex; $dimy = $sizey; $usethumbs = false; if (file_exists(dirname(__FILE__) . "/thumbs") && is_writable(dirname(__FILE__) . "/thumbs")) { // is a caching dir available and writeable? $cachename = dirname(__FILE__) . "/thumbs/" . sha1($image . $sizex) . ".jpg"; $usethumbs = true; } if ($usethumbs && file_exists($cachename)) { // we return the jpg! header("Content-type: image/jpg"); header("Content-Length: " . filesize($cachename)); header("Pragma: no-cache"); header("Expires: 0"); $fp = fopen($cachename, "rb"); while ($content = fread($fp, 8192)) { print $content; } fclose($fp); return true; } else { if (file_exists($image)) { $oldsize = getimagesize($image); // for broken images we try to read the exif data! if ($oldsize[0] == 0) { $oldsize = get_exif_size($image, $image); } $oldsizex = $oldsize[0]; $oldsizey = $oldsize[1]; if ($oldsizex < $sizex && $oldsizey < $sizey) { $sizex = $oldsizex; $sizey = $oldsizey; } $height = $sizey; $width = $height / $oldsizey * $oldsizex; if ($width > $sizex) { $width = $sizex; $height = $width / $oldsizex * $oldsizey; } if (isMemoryOk($oldsize, "")) { $src = get_image_src($image, $oldsize[2]); if (!$src) { // error in image! if ($sizex < 100) { // we return an empty white one ;). $src = ImageCreateTrueColor($oldsizex, $oldsizey); $back = imagecolorallocate($src, 255, 255, 255); imagefilledrectangle($src, 0, 0, $oldsizex, $oldsizex, $back); } debug($image . " is not a valid image - please check the file."); return false; } // $dst = ImageCreateTrueColor($width, $height); $dst = ImageCreateTrueColor($dimx, $dimy); if ($dimx < 100) { // white bg for small preview $back = imagecolorallocate($dst, $bg_color_preview_R, $bg_color_preview_G, $bg_color_preview_B); } else { // gray bg for big preview $back = imagecolorallocate($dst, 245, 245, 245); } imagefilledrectangle($dst, 0, 0, $dimx, $dimy, $back); if ($dimx > 100) { // border imagerectangle($dst, 0, 0, $dimx - 1, $dimy - 1, imagecolorallocate($dst, 160, 160, 160)); } $offsetx = 0; $offsetx_b = 0; if ($dimx > $width) { // we have to center! $offsetx = floor(($dimx - $width) / 2); } else { if ($dimx > 100) { $offsetx = 4; $offsetx_b = 8; } } $offsety = 0; $offsety_b = 0; if ($dimy > $height) { // we have to center! $offsety = floor(($dimy - $height) / 2); } else { if ($dimx > 100) { $offsety = 4; $offsety_b = 8; } } $trans = imagecolortransparent($src); imagecolorset($src, $trans, 255, 255, 255); imagecolortransparent($src, imagecolorallocate($src, 0, 0, 0)); imagecopyresampled($dst, $src, $offsetx, $offsety, $srcx, $srcy, $width - $offsetx_b, $height - $offsety_b, $oldsizex, $oldsizey); header("Content-type: image/jpg"); if ($usethumbs) { // we save the thumb imagejpeg($dst, $cachename, $compression); } if (!$generateOnly) { header("Pragma: no-cache"); header("Expires: 0"); ob_start(); if (imagejpeg($dst, "", $compression)) { $buffer = ob_get_contents(); header("Content-Length: " . strlen($buffer)); ob_end_clean(); echo $buffer; @imagedestroy($dst); return true; } else { ob_end_flush(); debug('cannot save: ' . $image); @imagedestroy($src); } } } } } return false; }
/** * resizes a file and writes it back to the user! - can do jpg, png and gif if the support is there ! * renamed png's (that that are actually jpg's are handled as well!) * Needs gdlib > 2.0! */ function send_thumb($image, $compression, $sizex, $sizey, $generateOnly = false) { global $bg_color_preview_R, $bg_color_preview_G, $bg_color_preview_B; global $info_text, $info_textcolor_R, $info_textcolor_G, $info_textcolor_B, $info_font, $info_fontsize; set_error_handler('on_error_no_output'); ini_set('gd.jpeg_ignore_warning', 1); // since php 5.1.3 this leads that corrupt jpgs are read much better! set_error_handler('on_error'); $srcx = 0; $srcy = 0; $dimx = $sizex; $dimy = $sizey; $usethumbs = false; if (file_exists(dirname(__FILE__) . '/thumbs') && is_writable(dirname(__FILE__) . '/thumbs')) { // is a caching dir available and writeable? $cachename = dirname(__FILE__) . '/thumbs/' . sha1($image . $sizex) . '.jpg'; $usethumbs = true; } if ($usethumbs && file_exists($cachename)) { // we return the jpg! header('Content-type: image/jpg'); header('Content-Length: ' . filesize($cachename)); $fp = fopen($cachename, 'rb'); while ($content = fread($fp, 8192)) { print $content; } fclose($fp); return true; } else { if (file_exists($image)) { if (filesize($image) == 0) { return false; } $oldsize = getimagesize($image); // for broken images we try to read the exif data! if ($oldsize[0] == 0) { $oldsize = get_exif_size($image, $image); } $oldsizex = $oldsize[0]; $oldsizey = $oldsize[1]; if ($oldsizex < $sizex && $oldsizey < $sizey) { $sizex = $oldsizex; $sizey = $oldsizey; } $height = $sizey; $width = $height / $oldsizey * $oldsizex; if ($width > $sizex) { $width = $sizex; $height = $width / $oldsizex * $oldsizey; } if (isMemoryOk($oldsize, $sizex, '')) { $src = get_image_src($image, $oldsize[2]); if (!$src) { // error in image! if ($sizex < 100) { // we return an empty white one ;). $src = ImageCreateTrueColor($oldsizex, $oldsizey); $back = imagecolorallocate($src, 255, 255, 255); imagefilledrectangle($src, 0, 0, $oldsizex, $oldsizex, $back); } tfu_debug($image . ' is not a valid image - please check the file.'); return false; } // $dst = ImageCreateTrueColor($width, $height); $dst = ImageCreateTrueColor($dimx, $dimy); if ($dimx < 100) { // white bg for small preview $back = imagecolorallocate($dst, $bg_color_preview_R, $bg_color_preview_G, $bg_color_preview_B); } else { // gray bg for big preview $back = imagecolorallocate($dst, 245, 245, 245); } imagefilledrectangle($dst, 0, 0, $dimx, $dimy, $back); if ($dimx > 100) { // border imagerectangle($dst, 0, 0, $dimx - 1, $dimy - 1, imagecolorallocate($dst, 160, 160, 160)); } $offsetx = 0; $offsetx_b = 0; if ($dimx > $width) { // we have to center! $offsetx = floor(($dimx - $width) / 2); } else { if ($dimx > 100) { $offsetx = 4; $offsetx_b = 8; } } $offsety = 0; $offsety_b = 0; if ($dimy > $height) { // we have to center! $offsety = floor(($dimy - $height) / 2); } else { if ($dimx > 100) { $offsety = 4; $offsety_b = 8; } } $trans = imagecolortransparent($src); imagecolorset($src, $trans, 255, 255, 255); imagecolortransparent($src, imagecolorallocate($src, 0, 0, 0)); imagecopyresampled($dst, $src, $offsetx, $offsety, $srcx, $srcy, $width - $offsetx_b, $height - $offsety_b, $oldsizex, $oldsizey); if (function_exists("imagettftext") && $dimx > 100 && $info_text != '') { // some extra info at the bottom of the image. Available parameters: {date} {size} {dimension} $text = str_replace('{dimension}', $oldsizex . "x" . $oldsizey, $info_text); $text = str_replace('{size}', formatSize(filesize($image)), $text); $text = str_replace('{date}', date("d.m.Y", filemtime($image)), $text); $color = imagecolorclosest($dst, $info_textcolor_R, $info_textcolor_G, $info_textcolor_B); imagettftext($dst, $info_fontsize, 0, 8, $dimy - 8, $color, $info_font, $text); } header('Content-type: image/jpg'); if ($usethumbs) { // we save the thumb imagejpeg($dst, $cachename, $compression); } if (!$generateOnly) { ob_start(); if (imagejpeg($dst, '', $compression)) { $buffer = ob_get_contents(); header('Content-Length: ' . strlen($buffer)); ob_end_clean(); echo $buffer; @imagedestroy($dst); return true; } else { ob_end_flush(); tfu_debug('cannot save: ' . $image); @imagedestroy($src); } } } } } return false; }