我有以下代码将文件移动到文件夹,然后使用该路径我在提交表单时裁剪图像。
<?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;
}
}
?>
现在如何将裁剪后的图像保存回文件夹(新/旧)并将路径保存在数据库中?
答案 0 :(得分:0)
要将图像保存到php中的文件,可以使用imagejpeg()函数,文件名(= path)作为第二个参数。类似于imagepng()和imagegif()。
imagejpeg($img_dist,"some_file.jpeg")
如果要在数据库中存储某些内容,则取决于数据库。例如。如果您使用某种形式的SQL,则需要http://www.w3schools.com/sql/sql_insert.asp。
答案 1 :(得分:0)
最后我得到了答案//首先上传img
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);
}
JPEG / PNG的相同代码