【问题标题】:Image imagejpeg() returns nothing. CakePHP图像 imagejpeg() 不返回任何内容。 CakePHP
【发布时间】:2012-05-14 07:54:20
【问题描述】:

我正在修改此插件以保存路径记录并制作缩略图并保存http://bakery.cakephp.org/articles/srs2012/2012/03/12/ajaxmultiupload_plugin_for_cake_2_0_x_and_2_1 我像这样修改了插件中的upload.php:

function save($path, $folder, $filename) {    
    $input = fopen("php://input", "r");
    $temp = tmpfile();
    $realSize = stream_copy_to_stream($input, $temp);
    fclose($input);

    if ($realSize != $this->getSize()){            
        return false;
    }
    $width = 290; 
    $height = 146;
    $target = fopen($path, "w");  
    //$this->Upload->setImage($this->Image);

    fseek($temp, 0, SEEK_SET);
    stream_copy_to_stream($temp, $target);  
    fclose($target);
    $val = $this->resizeImage($filename, $filename, $folder, $folder.'/small/', $width, $height, 100);
    $this->saveToDatabase(array('path' => $folder.$filename, 'thumb' => $folder.'/small/'.$filename));

    return true;
}

这是我用来保存的功能:

function saveToDatabase($data){
    $this->Image->save($data);
}

我添加以下内容来创建缩略图:

function resizeImage($src_img, $dst_img, $src_path2, $dst_path2, $dst_w, $dst_h, $dst_quality){
    //Stop and giving an error if the file does not exists.
    $src_path = 'img/';
    $dst_path = 'img/';
    $src_path .= $src_path2;
    $dst_path .= $dst_path2;
    if(file_exists($src_path . basename($src_img)) == false){
        echo 0;
    }
        //Get variables for the function.
            //complete path of the source image.
    $src_cpl = $src_path . basename($src_img);
    //return $src_cpl;
            //complete path of the destination image.
    $dst_cpl = $dst_path . basename($dst_img);
            //extension excl "." of the source image, in lowercase.
    $src_ext = strtolower(end(explode(".", $src_img)));
            //width and height sizes of the source image.
    list($src_w, $src_h) = getimagesize($src_cpl);

            //get type of image.
    //return 'IETS: '.$src_cpl.' :IETS';  
    $src_type = exif_imagetype($src_cpl);//
    //Checking extension and imagetype of the source image and path.
    if( ($src_ext =="jpg") && ($src_type =="2") ){
        $src_img = imagecreatefromjpeg($src_cpl);
    }else if( ($src_ext =="jpeg") && ($src_type =="2") ){
        $src_img = imagecreatefromjpeg($src_cpl);
    }else if( ($src_ext =="gif") && ($src_type =="1") ){
        $src_img = imagecreatefromgif($src_cpl);
    }else if( ($src_ext =="png") && ($src_type =="3") ){
        $src_img = imagecreatefrompng($src_cpl);
    }else{
        die('<p>The file "'. $src_img . '" with the extension "' . $src_ext . '" and the imagetype "' . $src_type . '" is not a valid image. Please upload an image with the extension JPG, JPEG, PNG or GIF and has a valid image filetype.</p>');
    }

    //Get heights and width so the image keeps its ratio.
    $x_ratio = $dst_w / $src_w;
    $y_ratio = $dst_h / $src_h;

        if( (($x_ratio > 1) || ($y_ratio > 1)) && ($x_ratio > $y_ratio) ){
        //If one of the sizes of the image is smaller than the destination (normal: more height than width).
        $dst_w = ceil($y_ratio * $src_w);
        $dst_h = $dst_h;
    }elseif( (($x_ratio > 1) || ($y_ratio > 1)) && ($y_ratio > $x_ratio) ){
        //If one of the sizes of the image is smaller than the destination (landscape: more width than height).
        $dst_w = $dst_w;
        $dst_h = ceil($x_ratio * $src_h);
    }elseif (($x_ratio * $src_h) < $dst_h){
        //if the image is landscape (more width than height).
        $dst_h = ceil($x_ratio * $src_h);
        $dst_w = $dst_w;
    }elseif (($x_ratio * $src_h) > $dst_h){
        //if the image is normal (more height than width).
        $dst_h = ceil($x_ratio * $src_h);
        $dst_w = $dst_w;
    }else{
        //if the image is normal (more height than width).
        $dst_w = ceil($y_ratio * $src_w);
        $dst_h = $dst_h;
    }
     // Creating the resized image.
    $dst_img=imagecreatetruecolor($dst_w,$dst_h);
    $result = imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w, $dst_h,$src_w,$src_h);

    // Saving the resized image.
    $result2 = imagejpeg($dst_img,$dst_cpl,$dst_quality);
    return 'dfdfhdfhhgfddghhgffddghdghhdgghgfhdh: '.$result.'|||asdasda'.$result2;

    // Cleaning the memory.
    imagedestroy($src_img);
    imagedestroy($dst_img);

}

enter code here

现在我已经在同一台服务器上使用了这段代码,它运行得非常好。但在我的返回值中,我得到以下信息:

[uploader] responseText = dfdfhdfhhgfddghhgffddghdghhdgghgfhdh: 1|||asdasda{"success":true}

函数 imagejpeg 不返回任何内容。

现在可能出了什么问题?我只是一个实习生,我不知道哪里出了问题。

您好, 伤害。 撞?

【问题讨论】:

    标签: php ajax cakephp upload


    【解决方案1】:

    请参阅http://php.net/manual/en/function.imagejpeg.php - 我认为您在函数 imagejpeg 中有问题 - 请参阅参数文件名。

    如果还有问题,请看http://www.php.net/manual/en/function.gd-info.php

    【讨论】:

    • 路径如下:img/uploads/Projects/2//small/1336986225.jpg ($dst_cpl) 这就是我通过gd_info得到的:["GIF Read Support"]= > bool(true) ["GIF Create Support"]=> bool(true) ["JPEG Support"]=> bool(true) ["PNG Support"]=> bool(true)另一个组件中的相同代码确实有效...
    • 你有 2 个斜线:img/uploads/Projects/2 ==> //
    • 不,这不是问题。我通过将 $dst_cpl 设置为硬编码字符串进行了仔细检查。除了代码在前面提到的另一个组件中的同一台服务器上工作之外,那里的路径是相同的。
    • 对不起,我对这个功能不熟悉...应该放在哪里,之后该怎么办?
    • 这个函数负责打印警告和错误。参数 -1 = 全部打印。把它放在 srcipt 的开头。
    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2020-04-27
    • 2019-04-06
    • 2012-02-15
    • 2020-09-07
    相关资源
    最近更新 更多