Creating a dynamic image with php and drupal - php

I am new to drupal and php. Right now I am trying to customise my own drupal page by editing the page.tpl.php template in a zen sub-theme, and so far all the php code related with data and text are working fine on the page.
However when I am trying to further customise the page by trying to add a dynamic image through php, things are not working. The site now returns a broken link icon which blocks everything else that should be displayed on the page. I have tested the code which generates the dynamic image on an individual php page and it is working well. I wonder how can I resolve this issue and let the dynamic image be properly displayed on my drupal site? Many thanks in advance.
Here is my code
<div id="projects">
<?php
global $node;
$projectname = db_query("SELECT title FROM {node}
where type='project' and uid= :uid", array(':uid' =>$user->uid))- >fetchAll();
if ($projectname){
foreach($projectname as $item) {
print $item->title ;
echo "<br>";
}
}
else{
print "no project";
}
?>
#this is the chunk of codes that generates the dynamic image
<?php
//$image = #imagecreate(200, 20)or die("Cannot Initialize new GD image stream");
$image = imagecreate(200, 20);
$background = imagecolorallocate($image,0,0,0);
$foreground = imagecolorallocate($image,255,255,255);
imagestring($image,5,5,1,"This is a Test",$foreground);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>
</div>

The image you generate need to be in its own page. imagejpeg will print the raw image binary output.
For your web html page to display it, you can import it with a basic <img src="">.
Resume:
image.php => render in full binary an image
page.php => standard web html page with <img src="image.php"> somewhere to fetch your dynamically generated image
Edit: I may have read the question to fast, it still not clear for me...
Most common issue are that errors or text outputs have been send with the image and corrupt the raw output. Removing the header to display temporary the output will help showing what have been send. Also care with <?php blocks, a single space before your php code will corrupt the image.
In short: image.php must only return raw binary image data.

Related

Create dynamic html page with php

I need to create new html pages with PHP. What I need is to get a new html page with a canvas inside.
At this moment I capture the canvas from another page with html2canvas.
I need this canvas to become the background of the new html page, or just a 100% width 100% height picture.
What I have with PHP is this, but I need to capture the picture or just the canvas on fly, and then make something like this:
<?php
$file = file_get_contents("example.html");
file_put_contents("example.html", $file);
?>
How can I do this? thanks.
From what I understand you're trying to take a screenshot of a page, then use that as the background of another new created page in PHP.
Take a look at this for taking screen shots: Website screenshots using PHP
Once you have your screenshot taken, just use something such as
$newpage = 'example.html';
$contents = file_get_contents($newpage);
$contents = "<html><style>background-image: '<?php echo //Your Saved Screenshot ?>'</style></html>";
file_put_contents($newpage, $contents);

How do I extract an image from a MEDIUMTEXT, MyISAM field? [duplicate]

I have a PHP function that does on-the-fly image resizing for thumbnail creation.
I am having trouble as it's just displaying raw image stream instead of the actual image.
My code is using a function called thumbnail:
$thumbnail = thumbnail($item['filename'], 209, 137);
imagejpeg($thumbnail);
I've tried putting in:
header("Content-type: image/jpeg");
However, this just expects the full page to be an image. I have absolutely no idea where to go from here, been working at it for a while. I'd rather not save the image to disk although it's looking like this might be necessary.
You either
Do it the normal way
This mean you point at one url, and serve the contents of one image:
<img src="myimage.php">
and myimage.php is a script that looks like:
header('Content-type: image/jpeg');
imagejpeg($thumbnail);
die;
This technique has the advantage of being.. the normal way of doing things.
OR
Output the image inline
Using data uris outputting the contents as a base64 encoded string
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This technique is most appropriate with small images.
It has the advantage of meaning all the images are in the main http request - at the (possibly sizable) disadvantage of making the page harder to edit/build and possibly negating the benefits of browser caching (unless the html page is itself cached).
Being normal is easier
Regarding this statement in the question:
However, this just expects the full page to be an image
That's right - if you do it the normal way you want to point at your php script with the src attribute of an image tag, and server only an image - i.e. the exact same response as if you were pointing at an image file with a browser.
Unless you have a good reason to do things a different way - the "normal" way is a good place to start.
You can point an html img tag to an php file.
<img src='thumbnail.php?file=<?php echo $item['filename']; ?>' />
Then on your php file you display the image and change the headers since all it is doing is displaying an image.
$thumbnail = thumbnail($_GET['filename'], 209, 137);
imagejpeg($thumbnail);
header("Content-type: image/jpeg");
You need to insert the image like you would a normal image in HTML and create the image in a separate PHP file:
image.php
<?php
$img = imagecreate(100,100); //Create an image 100px x 100px
imagecolorallocate($img, 255,0,0); //Fill the image red
header('Content-type: image/jpeg'); //Set the content type to image/jpg
imagejpeg($img); //Output the iamge
imagedestroy($img); //Destroy the image
?>
myWebpage.html
<img src="image.php" />

Displaying image from a folder

image displays in the browser whrn the following code is used.But when the code is used inside html,the image does not appear instead a small box appear.
<?php
header('Content-Type: image/jpeg');
readfile('http://localhost/picture013.jpg');
?>
for example when i use no image is displayed.
<html>
<body>
<?php
header('Content-Type: image/jpeg');
readfile('http://localhost/picture013.jpg');
?>
</body>
</html>
in short i wanted to display all the images from a folder.
Maybe you can try this one
<?php
$dir = "localhost/image"; // your folder name ex: image
$imgs = glob($dir ."/*.jpg"); // get your image files with .jpg
foreach ($imgs AS $i) {
echo "<img src='$i'>"; //
}
?>
Think about what this is doing for a moment. That file already exists, you're not inventing image data from nothing, so why are you not just creating an element and link to that image?
Put an <img src="some/location/picture013.jsp"> on the page with an echo/print instead, and whatever page needs to show that image will work just fine. In this case it's already a legal URL, so you can just add <img src="http://localhost/picture013.jpg"> to whatever page needs it.
If it wasn't, you could make the server simply resolve the location you're pointing to with an .htaccess rule, or simply have the file live in an already web-visible directory. There is no reason to make PHP proxy the image data when it's already a normal web-visible resource; in fact, you're just over-complicating your code with unnecessary steps.

Change PHP GD image output file url name/link

I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.

Adjust Image if page break occurs in mpdf library

I am generating pdf from html source using mpdf library in php and everything seems working perfect.
Now I have an issue with the images. Suppose before page end I'm inserting an image but image is big so that it doesn't fit at the bottom of first page and goes to second page. Now I have a long white space at the end of first page because image moved to second page.
Now I want is "if next item to insert in pdf is an image then calculate the remaining size of pdf page if it is less than Image size then adjust the image size so that image can be fit in the pdf page instead of moving to next page" how can i do this here?
Please check the issue image :
If any one has other solution please help me to sort out.
Here's My sample code
include_once 'simple_html_dom.php'; //import html dom and mpdf library
include 'PDFScript/MPDF/mpdf.php';
$mpdf = new mPDF('','','','',15,15,30,15,8,8); //create mpdf object
$html = new simple_html_dom(); //create html dom object
$html = file_get_html("htmlsource.html"); //htmlsource.html is a webpage can contain any html data
$mpdf->WriteHTML($html); //write html source to pdf
$mpdf->Output(); //generate pdf
I got a solution from mpdf forum itself. If Anyone also has the same problem enclose every image in your html inside table as table has a autosize feature in mpdf library.
For more information please check here

Categories