简体   繁体   中英

how to store back the cropped image in folder 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?

For saving an image to a file in php, you can use the imagejpeg() function, with the filename(=path) as second parameter. Similar for imagepng() and imagegif().

imagejpeg($img_dist,"some_file.jpeg")

If you want to store something in a database, that depends on the database. Eg if you use some form of SQL, then the http://www.w3schools.com/sql/sql_insert.asp would be needed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM