PHP gd创建图像,添加到文件夹和数据库

时间:2014-01-14 17:19:53

标签: 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 = 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并将文件保存到上传文件夹..

有可能吗?

感谢。

3 个答案:

答案 0 :(得分:0)

这可能就是你所需要的:

$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);

答案 1 :(得分:0)

我想你也可以添加这些代码:

$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";

答案 2 :(得分:0)

您需要一个名称和路线来保存图片:

// Save the image as 'test.jpg'
$name_image="test.jpg";
imagejpeg($im, 'folder_to_save_image/'.$name_image);

您可以将名称保存在数据库中

检查imagejpeg