douna1941 2015-07-31 12:31
浏览 43
已采纳

如何将裁剪后的图像存储在php文件夹中?

I have the following code to move file to folder and then with that path I am cropping the image on submitting the form.

<?php
if(isset($_POST['submit']))
{
    header('Content-type: image/jpeg');
    move_uploaded_file($_FILES['image_file'] \
    ['tmp_name'],'media/'.$_FILES['image_file']['name']);
    $path='media/'.$_FILES['image_file']['name'];
    $maxsize=200;
    $sourcefile=$path;
    $imgcomp=0;
    $g_imgcomp=100-$imgcomp;
    if(file_exists($sourcefile))
    {
        $g_is=getimagesize($sourcefile);
        if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize)
        {
            $new_width=$g_is[0];
            $new_height=$g_is[1];
        }
        else
        {
            $w_adjust = ($maxsize / $g_is[0]);
            $h_adjust = ($maxsize / $g_is[1]);
            if($w_adjust <= $h_adjust)
            {
                $new_width=($g_is[0]*$w_adjust);
                $new_height=($g_is[1]*$w_adjust);
            } 
            else
            {
            $new_width=($g_is[0]*$h_adjust);
            $new_height=($g_is[1]*$h_adjust);
            }
        }
        $image_type = strtolower(strrchr($sourcefile, "."));
        switch($image_type)
        {
          case '.jpg':
             $img_src = imagecreatefromjpeg($sourcefile);
             break;
          case '.jpeg':
             $img_src = imagecreatefromjpeg($sourcefile);
             break;
          case '.png':
             $img_src = imagecreatefrompng($sourcefile);
             break;
          case '.gif':
             $img_src = imagecreatefromgif($sourcefile);
             break;
          default:
             echo("Error Invalid Image Type");
             die;
             break;
        }
        $img_dst=imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width,
        $new_height, $g_is[0], $g_is[1]);
        imagejpeg($img_dst);
        imagedestroy($img_dst);
         $src_img = $image_create($sourcefile);
         rename('$src_img', 'media1/$src_img');
         ?>
         <img src="$src_img">// image is desplaying
    <?php } 
    else
    {
        return false;
    }
}
?>

Now how can I save the cropped image back to the folder(new/old) and save the path in data base?

  • 写回答

2条回答 默认 最新

  • dqbr37828 2015-08-01 06:50
    关注

    finally i got the answer here//upload img first

    move_uploaded_file($_FILES['file2']['tmp_name'],
    'upimg/'.$_FILES['file2']['name']);
    $add='upimg/'.$_FILES['file2']['name'];
    //Start the thumbnail generation
    $n_width=200;          // Fix the width of the thumb nail images
    $n_height=200;         // Fix the height of the thumb nail imaage
    ////////////////////////////////////////////
    $tsrc="thimg/".$_FILES['file2']['name'];
    // Path where thumb nail image will be stored
    if (@$_FILES['file2']['type']=="image/gif")
    {
    $im=ImageCreateFromGIF($add);
    $width=ImageSx($im);              // Original picture width is stored
    $height=ImageSy($im);                  // Original picture height is stored
    $n_height=($n_width/$width) * $height;
    // Add this line to maintain aspect ratio
    $newimage=imagecreatetruecolor($n_width,$n_height);
    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
    if (function_exists("imagegif"))
    {
        Header("Content-type: image/gif");
        ImageGIF($newimage,$tsrc);
    }
    elseif (function_exists("imagejpeg"))
    {
        Header("Content-type: image/jpeg");
        ImageJPEG($newimage,$tsrc);
    }
        chmod("$tsrc",0777);
    }
    

    Same code for JPEG/PNG

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误