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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器