Create and then directly upload an image from a folder embedded in HTML
I want to call image from dump folder after website
My page image.php
looks like this:
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
$text = $_GET['name'] ;
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);
imagedestroy($jpg_image);
?>
And I used Javascript on the home page that redirects me to result.php
with an image.
My page result.php
looks like this:
<html>
<body>
<img src="image.php?name=<?php echo $_GET['name']; ?>" />
</body>
</html>
Now I image.php
am calling the image from and I want to call it from the dump folder. Any help?
source to share
This is yours results.php
. First, it creates an image and saves it in the / dump folder, then the HTML is displayed. At this point, the image will be loaded directly from the / dump folder.
<html>
<body>
<?php
$text = $_GET['name'] ;
$image_url="dump/".rawurlencode(trim($text)).".jpg";
// Check if the file doesn't exist, create it
if (!file_exists($image_url)) {
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
imagejpeg($jpg_image,$image_url);
imagedestroy($jpg_image);
}
?>
<img src="<?php echo $image_url; ?>" />
</body>
</html>
Once this prototype works, you can add additional security.
source to share
I think you shouldn't do that.
1.) You use $_GET['name']
directly in the image tag. You shouldn't trust user input, so filter your variable first if you are using it in your image.
http://php.net/manual/en/function.filter-input.php
2.) Its much better when you create this image on load to keep performance. When you render every time you waste a lot of CPU.
So, it's better to keep the original image and create a cache and transform the image after loading, or use if in your method to check if the image is cached.
<?php
header('Content-type: image/jpeg');
$filename = rawurlencode(trim($text)).".jpg";
if(!is_file(__DIR__.'dump_cache/'.$filename)) {
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
$text = $_GET['name'] ;
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);
.... here write file to your cache folder .....
imagedestroy($jpg_image);
} else {
echo file_get_contents(__DIR__.'dump_cache/'.$filename);
}
?>
Something like that. When your image is not loading from the dump folder, try adding the full path to __DIR__
.
Something like that:
$image_url= __DIR__."/dump/".rawurlencode(trim($text)).".jpg";
if it's the same folder.
source to share