示例#1
0
function renderImage($data)
{
    $hash = $data['hash'];
    if ($data['changecode']) {
        $changecode = $data['changecode'];
        unset($data['changecode']);
    }
    $pm = new PictshareModel();
    $base_path = ROOT . DS . 'upload' . DS . $hash . DS;
    $path = $base_path . $hash;
    $type = $pm->isTypeAllowed($pm->getTypeOfFile($path));
    $cached = false;
    $cachename = $pm->getCacheName($data);
    $cachepath = $base_path . $cachename;
    if (file_exists($cachepath)) {
        $path = $cachepath;
        $cached = true;
    } else {
        if (MAX_RESIZED_IMAGES > -1 && $pm->countResizedImages($hash) > MAX_RESIZED_IMAGES) {
            //if the number of max resized images is reached, just show the real one
            $path = ROOT . DS . 'upload' . DS . $hash . DS . $hash;
        }
    }
    switch ($type) {
        case 'jpg':
            header("Content-type: image/jpeg");
            $im = imagecreatefromjpeg($path);
            if (!$cached) {
                if ($pm->changeCodeExists($changecode)) {
                    changeImage($im, $data);
                    imagejpeg($im, $cachepath, 95);
                }
            }
            imagejpeg($im);
            break;
        case 'png':
            header("Content-type: image/png");
            $im = imagecreatefrompng($path);
            if (!$cached) {
                if ($pm->changeCodeExists($changecode)) {
                    changeImage($im, $data);
                    imagepng($im, $cachepath, 1);
                }
            }
            imageAlphaBlending($im, true);
            imageSaveAlpha($im, true);
            imagepng($im);
            break;
        case 'gif':
            header("Content-type: image/gif");
            $im = imagecreatefromgif($path);
            readfile($path);
            break;
    }
    exit;
}
示例#2
0
function renderImage($data)
{
    $hash = $data['hash'];
    if ($data['changecode']) {
        $changecode = $data['changecode'];
        unset($data['changecode']);
    }
    $pm = new PictshareModel();
    $base_path = ROOT . DS . 'upload' . DS . $hash . DS;
    $path = $base_path . $hash;
    $type = $pm->isTypeAllowed($pm->getTypeOfFile($path));
    $cached = false;
    $cachename = $pm->getCacheName($data);
    $cachepath = $base_path . $cachename;
    if (file_exists($cachepath)) {
        $path = $cachepath;
        $cached = true;
    } else {
        if (MAX_RESIZED_IMAGES > -1 && $pm->countResizedImages($hash) > MAX_RESIZED_IMAGES) {
            //if the number of max resized images is reached, just show the real one
            $path = ROOT . DS . 'upload' . DS . $hash . DS . $hash;
        }
    }
    switch ($type) {
        case 'jpg':
            header("Content-type: image/jpeg");
            $im = imagecreatefromjpeg($path);
            if (!$cached) {
                if ($pm->changeCodeExists($changecode)) {
                    changeImage($im, $data);
                    imagejpeg($im, $cachepath, 95);
                }
            }
            imagejpeg($im);
            break;
        case 'png':
            header("Content-type: image/png");
            $im = imagecreatefrompng($path);
            if (!$cached) {
                if ($pm->changeCodeExists($changecode)) {
                    changeImage($im, $data);
                    imagepng($im, $cachepath, 1);
                }
            }
            imageAlphaBlending($im, true);
            imageSaveAlpha($im, true);
            imagepng($im);
            break;
        case 'gif':
            if ($data['mp4'] || $data['webm']) {
                $gifpath = $path;
                $mp4path = $base_path . 'mp4_1.' . $hash;
                //workaround.. find a better solution!
                $webmpath = $base_path . 'webm_1.' . $hash;
                if (!file_exists($mp4path) && !$data['preview']) {
                    //if mp4 does not exist, create it
                    $pm->gifToMP4($gifpath, $mp4path);
                }
                if (!file_exists($webmpath) && $data['webm'] && !$data['preview']) {
                    $pm->saveAsWebm($gifpath, $webmpath);
                }
                if ($data['raw']) {
                    if ($data['webm']) {
                        serveFile($webmpath, $hash . '.webm', 'video/webm');
                    } else {
                        serveFile($mp4path, $hash . '.mp4', 'video/mp4');
                    }
                } else {
                    if ($data['preview']) {
                        $file = $mp4path;
                        if (!file_exists($cachepath)) {
                            $pm->saveFirstFrameOfMP4($mp4path, $cachepath);
                        }
                        header("Content-type: image/jpeg");
                        readfile($cachepath);
                    } else {
                        renderMP4($mp4path, $data);
                    }
                }
            } else {
                if (!$cached && $data['size']) {
                    $pm->resizeFFMPEG($data, $cachepath, 'gif');
                }
                header("Content-type: image/gif");
                if (file_exists($cachepath)) {
                    readfile($cachepath);
                } else {
                    readfile($path);
                }
            }
            break;
        case 'mp4':
            if (!$cached && !$data['preview']) {
                $pm->resizeFFMPEG($data, $cachepath, 'mp4');
                $path = $cachepath;
            }
            if (file_exists($cachepath) && filesize($cachepath) == 0) {
                //if there was an error and the file is 0 bytes, use the original
                $cachepath = ROOT . DS . 'upload' . DS . $hash . DS . $hash;
            }
            if ($data['webm']) {
                $pm->saveAsWebm(ROOT . DS . 'upload' . DS . $hash . DS . $hash, $cachepath);
            }
            if ($data['raw']) {
                serveFile($cachepath, $hash, 'video/mp4');
            } else {
                if ($data['preview']) {
                    if (!file_exists($cachepath)) {
                        $pm->saveFirstFrameOfMP4($path, $cachepath);
                    }
                    header("Content-type: image/jpeg");
                    readfile($cachepath);
                } else {
                    renderMP4($path, $data);
                }
            }
            break;
    }
    exit;
}