Пример #1
0
function tfu_info($file)
{
    global $use_image_magic;
    unset($_SESSION['TFU_LAST_UPLOADS']);
    $_SESSION['TFU_LAST_PREVIEW'] = fixUrl(getRootUrl() . $file);
    echo '&size=' . filesize($file);
    // we check if the image can be resized
    if (is_supported_tfu_image($file)) {
        set_error_handler('on_error_no_output');
        // is needed because error are most likly but we don't care about fields we don't even know
        $oldsize = @getimagesize($file);
        set_error_handler('on_error');
        if ($oldsize) {
            if (isMemoryOk($oldsize, "")) {
                echo '&hasPreview=true&tfu_x=' . $oldsize[0] . '&tfu_y=' . $oldsize[1];
                // has preview!
            } else {
                echo '&hasPreview=error';
                // too big! - same error massage as hasPreview=false
            }
            return;
        }
        echo '&hasPreview=false';
        // no image!
    }
    if (preg_match("/.*\\.(p|P)(d|D)(f|F)\$/", $file) && $use_image_magic && file_exists(dirname(__FILE__) . '/thumbs') && is_writable(dirname(__FILE__) . '/thumbs')) {
        // check if pdf
        echo '&hasPreview=true&tfu_x=1000&tfu_y=1000';
        // has preview! - pdfs are max 1000x1000';
        return;
    }
    echo '&hasPreview=false';
}
Пример #2
0
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;
}
Пример #3
0
         // we return nothing if no image.
     }
 } else {
     if ($action == "info") {
         // get infos about a file
         unset($_SESSION["TFU_LAST_UPLOADS"]);
         $_SESSION["TFU_LAST_PREVIEW"] = fixUrl(getRootUrl() . $file);
         echo "&size=" . filesize($file);
         // we check if the image can be resized
         if (is_supported_tfu_image($file)) {
             set_error_handler("on_error_no_output");
             // is needed because error are most likly but we don't care about fields we don't even know
             $oldsize = @getimagesize($file);
             set_error_handler("on_error");
             if ($oldsize) {
                 if (isMemoryOk($oldsize, "")) {
                     echo "&hasPreview=true&tfu_x=" . $oldsize[0] . "&tfu_y=" . $oldsize[1];
                     // has preview!
                 } else {
                     echo "&hasPreview=error";
                     // too big! - same error massage as hasPreview=false
                 }
                 return;
             }
             echo "&hasPreview=false";
             // no image!
         }
     } else {
         if ($action == "text") {
             // get infos about a file
             if (is_writable($file)) {