how to create image from data:image in php code example

Example 1: php create an image

<?php
header("Content-Type: image/png");//change the php file to an image
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");//creates an image with the resolution x:110 y:20 
$background_color = imagecolorallocate($im, 0, 0, 0);//create an color with RGB
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);//draws text to the image with the font:1 xpos:5 ypos:5 
imagepng($im);//sends the image data to the user
imagedestroy($im);//destroys the image from the server
?>

Example 2: how to store and retrieve image from database in php

// Get the name of images
  	$Get_image_name = $_FILES['image']['name'];
  	
  	// image Path
  	$image_Path = "images/".basename($Get_image_name);

  	$sql = "INSERT INTO student_table (imagename, contact) VALUES ('$Get_image_name', 'USA')";
  	
	// Run SQL query
  	mysqli_query($conn, $sql);

  	if (move_uploaded_file($_FILES['image']['tmp_name'], $image_Path)) {
  		echo "Your Image uploaded successfully";
  	}else{
  		echo  "Not Insert Image";
  	}
  }

Tags:

Php Example