裁剪图像后,它是黑色的

时间:2015-07-25 11:38:05

标签: php jcrop

裁剪图像后,它是黑色的。如果可以,请你帮助我。为什么要保存背面图像?发生了什么问题?也许我需要某种图像库?以下是我到目前为止所做的事情:

<?php
    if($_POST['frm'] == 'desktop' ) {
        $file_cnt   = $_POST['file'];
        $file_size  = filesize($file_cnt);
        $upload_dir = ABSPATH;
        $path       = $upload_dir.'wp-content/themes/poster/destop_imag';

        define('DIRECTORY', $path );
        $img        = rand().time().'.jpg';
        $upload_pt  = $path.'/'.$img;

        if(empty($_POST['wl']) && empty($_POST['hl'])) {
            //move_uploaded_file($file_cnt, $upload_pt);
            $content    = file_get_contents($_POST['file']);
            file_put_contents(DIRECTORY.'/'.$img.'', $content);
        } else {

            $targ_w         = $_POST['wl']; $targ_h = $_POST['hl'];
            $jpeg_quality   = 100;
            $path_parts     = pathinfo($img);
            $newfilename    = $_SERVER['DOCUMENT_ROOT'].'/poster/wp-content/themes/poster/destop_imag/'.$img;

            if ($path_parts['extension'] == 'jpg') {
                $img_r  = imagecreatefromjpeg($src);
                $dst_r  = ImageCreateTrueColor( $targ_w, $targ_h );

                imagecopyresampled($dst_r,$img_r,0,0,$_POST['xl'],$_POST['yl'],$targ_w,$targ_h,$_POST['wl'],$_POST['hl']);

                //header('Content-type: image/jpeg');
                imagejpeg($dst_r,$newfilename,$jpeg_quality);
                //echo '<script>window.location.href="'.get_page_link(27).'?crop_stat=1"</script>';
                //exit;
            } elseif ($path_parts['extension'] == 'png') {
                $img_r  = imagecreatefrompng($src);
                $dst_r  = ImageCreateTrueColor( $targ_w, $targ_h );

                imagecopyresampled($dst_r,$img_r,0,0,$_POST['xl'],$_POST['yl'],
                $targ_w,$targ_h,$_POST['wl'],$_POST['hl']);

                //header('Content-type: image/jpeg');
                imagepng($dst_r,$newfilename);
                echo '<script>window.location.href="'.get_page_link(27).'?crop_stat=1"</script>';
                exit;
            } elseif ($path_parts['extension'] == 'gif') {
                $img_r = imagecreatefromgif($src);
                $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

                imagecopyresampled($dst_r,$img_r,0,0,$_POST['xl'],$_POST['yl'],
                $targ_w,$targ_h,$_POST['wl'],$_POST['hl']);

                //header('Content-type: image/jpeg');
                imagegif($dst_r,$newfilename);
                echo '<script>window.location.href="'.get_page_link(27).'?crop_stat=1"</script>';
                exit;
            } else {
                echo "Source file is not a supported image file type.";
            }
        }
    }
?>

的Javascript

<script type="text/javascript">
    $(function(){
        $('#cropbox').Jcrop({
            aspectRatio: 1,
            onSelect: updateCoords,
            minSize : [329,329]
        });
    });

    function updateCoords(c)
        {
            $('#x').val(c.x);
            $('#y').val(c.y);
            $('#w').val(c.w);
            $('#h').val(c.h);
        };

    function checkCoords()
        {
            if (parseInt($('#w').val())) return true;
            alert('Please select a crop region then press submit.');
            return false;
        };
</script>

1 个答案:

答案 0 :(得分:0)

您需要在服务器上安装gd库:

对于CentOS:

sudo yum install php-gd

对于Debian和Ubuntu:

sudo apt-get install php5-gd

您应该像这样上传文件:                       上传新图片:
        
             

另外,不要试图从文件结尾猜测图像MIME类型,而是直接得到它的MIME类型:

if($_FILES['userfile']['type']=="image/jpeg") $src_image = imagecreatefromjpeg($uploaddir.$filename);
if($_FILES['userfile']['type']=="image/png") $src_image = imagecreatefrompng($uploaddir.$filename);

但我认为你的错误来自错误的路径:

file_put_contents(DIRECTORY.'/'.$img.'', $content);

保存文件。 但你加载

$img_r  = imagecreatefrompng($src);

什么是$src?它在我的代码中看到它是空的,因此您的图像为空,这会在复制后产生黑色图像。

使用上面的表单,您可以像这样处理图像:

$goodimage = 0;
$upload_dir = ABSPATH.'wp-content/themes/poster/destop_imag/';
if(is_array($_FILES['userfile'])) {
    $filename = basename($_FILES['userfile']['name']);
    $uploadfile = $upload_dir . $filename;
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        if($_FILES['userfile']['type']=="image/jpeg" || $_FILES['userfile']['type']=="image/png") {
    if($_FILES['userfile']['type']=="image/jpeg") $src_image = imagecreatefromjpeg($uploaddir.$filename);
    if($_FILES['userfile']['type']=="image/png") $src_image = imagecreatefrompng($uploaddir.$filename);
    $goodimage = 1;
        } else {
            $error = "Wrong file format: ".$_FILES['userfile']['type']."<br>Please upload in .jpg or .png format.";
        }
    }
}

$newheight = 100;
$x = imagesx($src_image);
$y = imagesy($src_image);
$dst_image_content = imagecreatetruecolor($x/($y/$newheight),$newheight);
imagecopyresampled($dst_image_content, $src_image, 0, 0, 0, 0, $x/($y/$newheight), $newheight, $x, $y);
imagejpeg($dst_image_content, $upload_dir."resized-".$filename,99);

代码加载上面表单提交的图像并将其大小调整为100px高度,然后使用文件名“resized - ”保存它。$ filename