从mySQL的base64_decode生成缩略图

时间:2012-02-14 12:14:45

标签: php mysql image base64 cakephp-2.0

我试图从mySQL数据库生成base64_decode生成缩略图的代码,但问题是为什么它说文件无效JPEG我确定源文件是纯JPG文件格式。那我无法解决:(

源文件(BASE64_ENCODE): http://pastebin.com/wFBcd79B

image.php in view / Helper文件夹代码:

<?php
class ImageHelper extends Helper {
    var $helpers = array('Html');
    var $cacheDir = 'imagecache';

    function getfile($i) {

        preg_match_all("/data:(.*);base64,/", $i, $temp_imagetype);

        $imagetype = $temp_imagetype[1][0];

        $image = base64_decode(preg_replace("/data.*base64,/","",$i));

        //echo $image;

        ob_start();

        //header("Content-type: ".$imagetype);
        header('Content-Type: image/jpeg');
        print($image);

        $data = ob_get_clean();

        //file_put_contents($this->webroot.'tmp/temp.jpg', 'test' );

        file_put_contents(ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'tmp/temp.jpg', $data );


        //return ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'tmp/temp2.jpg';
        //return 'temp2.jpg';
        return 'temp.jpg';
    //}
    }


    //source from: http://bakery.cakephp.org/articles/hundleyj/2007/02/16/image-resize-helper and modified for base64_decode
    function resize($path, $width, $height, $aspect = true, $htmlAttributes = array(), $return = false) {
        $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type 

        //$fullpath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.$this->themeWeb.IMAGES_URL;

        $fullpath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.IMAGES_URL;

        $temppath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'tmp/';


        //$formImage = imagecreatefromstring(base64_decode(preg_replace("/data.*base64,/","",$path)));

        //$url = $formImage;

        //$path = $this->getfile($path);

        $url = $temppath.$path;

        //$url = preg_replace("/data.*base64,/","",$path);

        //$url = base64_decode($url);

        if (!($size = getimagesize($url)))
            return; // image doesn't exist

        if ($aspect) { // adjust to aspect.
            if (($size[1]/$height) > ($size[0]/$width))  // $size[0]:width, [1]:height, [2]:type
                $width = ceil(($size[0]/$size[1]) * $height);
            else
                $height = ceil($width / ($size[0]/$size[1]));
        }

        $relfile = $this->cacheDir.'/'.$width.'x'.$height.'_'.basename($path); // relative file
        $cachefile = $fullpath.$this->cacheDir.DS.$width.'x'.$height.'_'.basename($path);  // location on server

        if (file_exists($cachefile)) {
        $csize = getimagesize($cachefile);
        $cached = ($csize[0] == $width && $csize[1] == $height); // image is cached
        if (@filemtime($cachefile) < @filemtime($url)) // check if up to date
            $cached = false;
        } else {
            $cached = false;
        }

        if (!$cached) {
            $resize = ($size[0] > $width || $size[1] > $height) || ($size[0] < $width || $size[1] < $height);
        } else {
            $resize = false;
        }

        if ($resize) {
            $image = call_user_func('imagecreatefrom'.$types[$size[2]], $url);
            if (function_exists("imagecreatetruecolor") && ($temp = imagecreatetruecolor ($width, $height))) {
                imagecopyresampled ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
            } else {
                $temp = imagecreate ($width, $height);
                imagecopyresized ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
            }
                call_user_func("image".$types[$size[2]], $temp, $cachefile);
                imagedestroy ($image);
                imagedestroy ($temp);
        }
        return $this->output(sprintf($this->Html->image($relfile,$htmlAttributes)));
    }
}
?>
视图文件夹代码中的

'index.php':

<?php
    foreach ($products as $product):
        echo $this->Image->resize($this->Image->getfile($product['Product']['image1']), 120, 120, false); 
    endforeach;
    ?>

输出是错误日志:

> Warning (2): imagecreatefromjpeg() [function.imagecreatefromjpeg]:
> gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file 
> [APP/View/Helper/image.php, line 86] 
> Warning (2): imagecreatefromjpeg() [function.imagecreatefromjpeg]:
> '/Users/user/Sites/mycakeapp/app/webroot/tmp/temp.jpg' is not a valid
> JPEG file [APP/View/Helper/image.php, line 86] 
> Warning (2): imagecopyresampled() expects parameter 2 to be resource, boolean given
> [APP/View/Helper/image.php, line 88] 
> Warning (2): imagedestroy() expects parameter 1 to be resource, boolean given
> [APP/View/Helper/image.php, line 94]

1 个答案:

答案 0 :(得分:0)

您链接的base64编码数据长度恰好是2¹⁶(65536)字节。这个数字太圆而不是巧合。

数据库列是否定义为TEXT?将其转换为更大的内容,例如MEDIUMTEXTLONGTEXT

将图像存储在文件系统中会更有意义。虽然数据库可以存储任意数据(如文件),但存在一些限制,使其不切实际:必须通过网络将大量数据从数据库服务器传输到应用程序,因此存在性能问题。庞大的数据库需要更长时间才能备份。您可能需要修改数据库设置:

  

BLOB或TEXT对象的最大大小由其类型决定,但实际可以在客户端和服务器之间传输的最大值取决于可用内存量和通信缓冲区的大小。您可以通过更改max_allowed_packet variable的值来更改消息缓冲区大小,但必须同时为服务器和客户端程序执行此操作。

http://dev.mysql.com/doc/refman/5.0/en/blob.html