php在压缩图像方面很有意思

时间:2017-07-14 02:47:08

标签: php

我很难搞定这项工作:

if(isset($_POST['submit']) and isset($_GET['add']))
    {

       $category_image=rand(0,99999)."_".$_FILES['category_image']['name'];
       *************************issue here **************
       //Main Image
       $tpath1='images/'.$category_image;          
       $pic1=compress_image($_FILES["category_image"]["tmp_name"], $tpath1, 80);

        //Thumb Image 
       $thumbpath='images/thumbs/'.$category_image;     
       $thumb_pic1=create_thumb_image($tpath1,$thumbpath,'200','200');   
      ***********************************************************

       $data = array( 
                'category_name'  =>  $_POST['category_name'],
               'category_image'  =>  $category_image
                );      

        $qry = Insert('tbl_category',$data);    

        $cat_id=mysqli_insert_id($mysqli);  

       if(!is_dir('categories/'.$cat_id))
       {

            mkdir('categories/'.$cat_id, 0777);

            mkdir('categories/'.$cat_id.'/thumbs', 0777);
       }            

        $_SESSION['msg']="10";

        header( "Location:manage_category.php");
        exit;   
    }

   I'm using a ubuntu 14.04 , apache24 web server and php 5.6 .

我使用以下代码添加类别,但我收到的网页内容内容Html 500 HTTP响应代码。   我没有收到任何错误,但我相信错误来自于compress_image方法:

function compress_image($source_url, $destination_url, $quality) 
{

    $info = getimagesize($source_url);

        if ($info['mime'] == 'image/jpeg')
              $image = imagecreatefromjpeg($source_url);

        elseif ($info['mime'] == 'image/gif')
              $image = imagecreatefromgif($source_url);

      elseif ($info['mime'] == 'image/png')
              $image = imagecreatefrompng($source_url);

        imagejpeg($image, $destination_url, $quality);

    return $destination_url;
}

我真的坚持这个可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好了我给了tmp文件夹的文件夹权限

chown -R root:www-data /var/tmp 

我仍然遇到问题我使用简单的try catch启用了错误日志,我收到了错误

调用未定义的函数imagecreatefromjpeg()

我通过安装和启用gd模块解决了,因为我使用php5.6我必须安装模块:

apt-get install php5.6-gd 

我必须启用它:

extension=php_gd2.dll