Can't show image with GD library PHP - php

I'm trying to display an image from another server with the code below
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("https://i.imgur.com/aAnTsd8.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
The above code works fine, but when I change the URL of the image to another URL, example that I put below, the code does not display the image.
https://www.wordapp.com/wpsite/wp-content/uploads/2017/01/google-ranking-factors.png
Does anyone know the reason for displaying one image and not displaying the other?
Thank you

Related

imagejpeg not working when used in Wordpress

Good day. Why is that when I try to run my code on an external page, it works perfectly. But when I used wordpress to add it on my page, it gives me strange errors. Why is that and how do I fix that?
code:
<?php
// (A) OPEN IMAGE
$img = imagecreatefromjpeg('https://images.unsplash.com/photo-1550684376-efcbd6e3f031?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80');
// (B) WRITE TEXT
$white = imagecolorallocate($img, 255, 255, 255);
$txt = "sad";
$font = realpath('arial.ttf');
//(IMAGE, FONT SIZE, TILT ANGLE, X, Y, COLOR, FONT, TEXT)
imagettftext($img, 12, 0, 253, 234, $white, $font, $txt);
// (C) OUTPUT IMAGE
header('Content-Type: image/jpeg');
imagejpeg($img);
// OR SAVE TO A FILE
// THE LAST PARAMETER IS THE QUALITY FROM 0 to 100
imagejpeg($img, "test.jpg", 100);
?>
Here's the result I got when trying it on my wordpress page:
Image here
The issue is that you are running this in a shortcode and change the header of the output, that you already created, namely the page. It depends on what you want to do, if you want to display the image in the site:
ob_start();
imagejpeg( $img, NULL, 100 );
imagedestroy( $img );
$i = ob_get_clean();
echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>";

Creating image and loading it in the page

I want to create/generate an image with PHP, adding text into it, but without saving it into the FTP, I'd like to load it on the site by changing the content type to image/png, the same way I did it with ASP.NET:
Response.ContentType = "image/png";
rImage.Save(Response.OutputStream, ImageFormat.Png);
I found a function called file_put_contents, but I'm not so sure this is what I'm looking for. If you know how to do something like this, please tell me.
I'm trying this code, but it fails to load the image, and shows the browser's default error image.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
Below example will regenerate an image with text over it:
<?php
header("Content-type: image/png"); // Set the header so that browser can output the image
$string = 'Print this text on Image'; // Text to be added on the image
$im = imagecreatefrompng("images/button1.png"); // Take base image
$orange = imagecolorallocate($im, 220, 210, 60); // Allocate a color for an image
imagestring($im, 3, 0, 0, $string, $orange); // Write the string at the top left
imagepng($im); // Create new image
imagedestroy($im); // destroy object
?>
Reference: http://php.net/manual/en/image.examples-png.php

Add Dynamic Text to images using GD Library or any other plugin in PHP

I want to make an app where user can see their meaning of name. I am done with the php part its just I am stuck how to output it to good looking image. I have used PHP image magician library before. But I am still not able to get it in the format I wanted.
Any help will be useful.
CHECK the image format below
https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDPCj8lZUELWsXD&w=470&h=246&url=http%3A%2F%2Fs3.alegra.me%2Fname%2Fnombre.php%3Fnombre%3Djayanta&cfs=1&upscale
$im = imagecreatefromjpeg('xc7ci4ptuxmgy3nd0brz.jpg');
$font="tahoma.ttf";
$red = imagecolorallocate($im, 255,0,0);
$string="JAYANTA";
$last_string= substr( $string, 1 );
$temp_x = 105;
$down_string[0]="JUST";
$down_string[1]="AMUSING";
$down_string[2]="YOUNG";
$down_string[3]="AMUSING";
$down_string[4]="NICE";
$down_string[5]="TOUGH";
$down_string[6]="AMUSING";
$bbox = imagefttext($im, 80, 0,90, 190, $red, $font, $string);
$temp_y=205; //y cord. of $down_string
for($x=1;$x < count($down_string);$x++){
for($y=0;$y < count($down_string);$y++){
$bbox = imagefttext($im, 12, 0, $temp_x, $temp_y, $red, $font, $down_string[$y][$x]);
$temp_x += 56+$y*1.8;
}
$temp_x = 105;
$temp_y+=15;
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
As you can see, ther is a lot of problems, but the solution is in the imagefttext function.

How to draw a vertical line on image in php [duplicate]

This question already has answers here:
PHP GD how to draw text over a line
(3 answers)
Closed 9 years ago.
i am making an antibot that displays different pictures, and asks the user to click on a specific picture. However, i would like some small random change to be made to the picture each time, so that software could not analyze and determine which picture is beeing displayed...
I would like one horizontal and one vertical line to be added at random coordinates with random color to the picture each time, then display the picture using get_file_contents and header.
Hope this makes sense... I would not want the changes to be saved to the picture, but only displayed to the user... I am using file_get_contents and header to display the picture, like this:
$id = $_GET['id'];
$image = "images/".$id . ".jpg";
$content = file_get_contents($image);
header('Content-Type: image/jpeg');
echo $content;
exit()
Thanks...
Using GD within PHP would allow you to do this
http://php.net/manual/en/book.image.php
create a file called image.php
From the php manual:
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
You could add a line by using the imageline function
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imageline($im, $x1, $y1, $x2, $y2, $orange);
imagepng($im);
imagedestroy($im);
?>
There are so many PHP GD functions available to achive this I think below code snippet may help you
<?php
$im = imagecreatefrompng("images/yourImage.png");
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
// Draw a vertical dashed line
imagedashedline($im, 50, 25, 50, 75, $white);
// Save the image
imagepng($im, './imagewithdashedline.png');
imagedestroy($im);
?>
reference PHP GD imagedashedline function

How to draw a png image in php?

$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
HTH!

Categories