PHP gd创建图像,添加到文件夹和数据库
我创建了这个图像:PHP gd创建图像,添加到文件夹和数据库,php,gd,Php,Gd,我创建了这个图像: header('content-type: image/jpeg'); //Load our base image $image = imagecreatefrompng(BASEPATH . '../images/blogMainImage.png'); //Setup colors and font file $white = imagecolorallocate($image, 255, 255, 255); $black = i
header('content-type: image/jpeg');
//Load our base image
$image = imagecreatefrompng(BASEPATH . '../images/blogMainImage.png');
//Setup colors and font file
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$font_path = BASEPATH . '../fonts/ACME Explosive.ttf';
//Get the positions of the text string
$text = wordwrap($_POST['title'], 15, "\n");
//Create Month
imagettftext($image, 16, 0, 20, 40, $black, $font_path, $text);
//Create final image
imagejpeg($image, '', 100);
//Clear up memory;
imagedestroy($image);
我已经成功地创建了图像。现在我需要做的是获取创建的图像文件名,将文件名保存到db,并将文件保存到上载文件夹
可能吗
谢谢。这可能是您需要的:
$name ='myimage.png';
// make sure you create a folder called **images** in the directory where the script is and give it write permissions.
$save_path = realpath(dirname(__FILE__).'/images/'.$name);
// I assume that you want to use the image in a website.
$imageurl = "http://www.mysite.com/images/$name";
imagejpeg($im, $save_path); // saves the image to **this_dir/images/myimage.png**
imagedestroy($im);
关于在数据库中存储文件名,您需要先设置数据库中的文件名,然后使用如下内容:
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO Photos(save_path, imageurl)
VALUES ("$save_path", "$imageurl")");
mysqli_close($con);
我认为您还可以添加以下代码:
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
mysql_query("UPDATE lms_users SET userPic='$actual_image_name' WHERE userId='$_SESSION[email]'");
}
else
echo "failed";
您需要名称和路径来保存图像:
// Save the image as 'test.jpg'
$name_image="test.jpg";
imagejpeg($im, 'folder_to_save_image/'.$name_image);
您可以将名称保存在数据库中
检查我认为必须有一个header函数来执行此操作。。我在网上搜索。。但是什么都没有…那你需要什么?