How to convert <img> to AMP in PHP - php

I have problem when covert old html code to AMP version:
My document with many image, but only have width value in source code.
<img src="/url-img1.jpg" with="728"></img>
<img src="/url-img2.jpg" with="640"></img>
In AMP version i need add height value as below:
<amp-img src="/url-img1.jpg" width="{widht-of-image}" height="{height-of-image}" layout="responsive"></amp-img>
<amp-img src="/url-img2.jpg" width="{widht-of-image}" height="{height-of-image}" layout="responsive"></amp-img>
Can anyone pls tell me how to do it?

You can use getimagesize()
list($width, $height) = getimagesize('path_to_the_image/url-img1.jpg');
echo '<amp-img src="/url-img1.jpg" width="'. $width .'" height="'. $height . '" layout="responsive"></amp-img>';
However, this defeats a bit the purpose of AMP which is that the resources are loaded at the end. Caching on their side should solve these issues, but still my advise is that you are hardcoding the width, you also hardcode the height. You could automate this once and then replace them for all.

Related

TCPDF don't display image with writeHTML

this question seems an evergreen on TCPDF...
I have an issue that's driving me crazy.
I have an html code that I use as "template" for my PDF, here I have my company logo.
Everything works fine on localhost (Windows), but when I move online, the image is not shown.
Pay attention: I don't get any error (ie the Unable to get image error) on my PDF the image is simple blank!
Infact if I click on the PDF on the position where the images it's supposed to be, I can select it, and Adobe enables the option "Copy image".
Obviously the image exists, is here, and permission are correct.
If I try to surf there, or view the generated HTML page, everything is fine.
This is the PHP code:
$pdf->SetMargins($params->get('pdfMarginLeft', 15), $params->get('pdfMarginTop', 27), $params->get('pdfMarginRight', 15));
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 8);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
Then this is my HTML code (I just skipped everything except the image):
<img alt="logo black" src="../images/logo_black.png" height="60" width="210" />
I've tried with the url (relative and absolute) and with the path (relative and absolute), the problem still occurs.
Any ideas?
This wasn't your problem, but it's a possible solution for people with a similar issue in the future. Please make sure the HTML attributes have double quotes.
$html = "<img src='...' />"; // This will not work
$html = '<img src="..." />'; // This will work
As things are working locally so you may try changing the image type from png to jpg and check after modifying your code accordingly and uploading the jpg on the server.
You can convert image type "jpg/png" to base64.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...."/>
this may help you !
In my case, I tried every solution above to no avail. It turned out I was missing width and height attributes. Adding those in, and using a root path ended up working for me:
<img src="/images/image.png" width="50" height="50"/>
In my case IMG tag not working until I write full path to file
Not working
<img src="/pdfrender/XXX.jpg" width="50" height="50">
Working (localhost example)
<img src="http://site.local/pdfrender/XXX.jpg" width="50" height="50">
It is also posible add image data inline as base64:
<img src="#Base64encodedImageFile" />
I implemented a str_replace for the image src, and that works ok now.
$html = str_replace("../images", $_SERVER["DOCUMENT_ROOT"] . '/images', $html);
I had to 'resave' the images:
$image = 'images/logo_example.png';
imagepng(imagecreatefrompng($image),$image);
Then it worked.
Not really an answer but as everybody is trying to add a "brick" here is a very strange detail I'm facing:
I have small "strips" of HTML that I put, one after other on a page. If I put the first one, which has 2 png and a small text, on the file, the png are visibles on the PDF.
If I put the second one, which has 2 png and a small text, on the file, the png are visible on the PDF.
Now, if I put the two, ONLY the first one as the images. The second one as blank picture. If I change the picture of the second strip, images became visible.
Detail: 1st and 2nd strips are using the same images!
Notice this seems to happened ONLY with PNG image. So it seems there is a bug and you can't have two time the same PNG image on a page. Using JPG solve the problem.
I had the same problem, locally it was working and on server blank image. I found out that htaccess password was the problem. I've put .htaccess file with the row Satisfy any to the folder with pictures and now it is working.

Get images height, before they are loaded

So here is my problem, I know the width of the images, but I don't know the height, and I'm using a preloader, my problem is that when the images are loading...the content under the images bounces down...when it's fully loaded, If I had fixed images width&height it will be easy to solve this, just set them using width and height attributes and it will look ok.
But is there a way to set the height before the page is rendered, maybe using PHP or so.
You can use getimagesize() on the server side with php to get image height and add that data properly to html. If you have too many images and you don't want to go though them every time the page loads, then consider caching this data in database somewhere
Well, you can get the hight and width via imagesx and imagesy then print it.
<?php
$img = 'image.jpg';
$image = imagecreatefromjpeg($img);
$width = imagesx($image);
$height = imagesy($image);
print '<img src="'.$img.'" height="'.$height.'" width="'.$width.'" alt="" />';
?>

Resizing div after iframe change src

So I automatically resize my iframe based on content height as follows:
<iframe name="main" id="main" src="main.php" frameborder=0 scrolling="no" onload="this.style.height = main.document.body.scrollHeight + 5; this.style.width = main.document.body.scrollWidth">
However, I also use a link to change the src (utilizing target if anyone was wondering), but my iframes vary heights vastly, and after changing back to the shorter one, my methods of resizing the div/document body haven't worked...
ex.
<a href="mail.php" onclick="document.getElementById("content").style.height = main.document.body.scrollHeight" target="main">
Any suggestions?
Div resize was working fine, provided I fix the double quotes...the problem was that the iframe wasn't resizing after changing source, got it working now though...Thanks

PHP crop and resize image on the fly

I have a web page that displays images that I don't know their size in advance. I was trying to use the GD functions to make the script resize and crop the images from me " Just before they are displayed.. I don't need caches" but I failed.
I need a script that I can call like this
<img src="display.php?src=blablabla&height=100&width=200" ?>
or even by calculating the width and height of css to preserve the proportions and make the image touch the box from inside like
<img src="blabla.jpg" style="height:<?php echo $height; ?>; width:<?php echo width; ?>" />
I don't need any sort of caching. How can I do that ?
WideImage rlz! :)
The resize's like that:
header('Content-type: image/jpeg');
echo WideImage::load('image.jpg')->resize(200, 100)->asString('jpg', 80);
// image.jpg resized at 200x100 with 80% of quality
You'll need to use the first style. Because this would be happening server-side, you can't check the CSS to get the desired size.
You just need to use the GD functions to open the appropriate file, use imagecopyresampled() to resize it, and then output to the buffer using imagejpeg. Don't forget to set the right headers:
header('Content-type: image/jpeg');
OR phpthumb http://phpthumb.sourceforge.net/
Demo is available at: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php
You are looking for TimThumb (Demo | Source Code):
Simply copy the source code into a new
document called ‘timthumb.php’, place
it in a folder on your site (ex:
/scripts/) and call the image like
this:
<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">

Rounded corners on images using PHP?

Does anyone know how to make a image have rounded corners using a PHP script?
It can be done using php-gd, but I ended up passing that task to the browser, using CSS:
<img src="photo.jpg" width="42"
height="42" alt="My cool photo"
style="border-radius: 15px;
-moz-border-radius: 15px;" />
Download easyphpthumbnail.class.php from this link
from this you can resize and convert image into rounded image.
in below example image is converted into transparent circle image.
include_once('easyphpthumbnail.class.php');
$source = 'demo.jpg';
$width = 100;
$height = 100;
$thumb = new easyphpthumbnail;
$thumb -> Thumbheight = $width;
$thumb -> Thumbwidth = $height;
$thumb -> Backgroundcolor = '#FFFFFF';
$thumb -> Clipcorner = array(2,50,0,1,1,1,1);
$thumb -> Maketransparent = array(1,0,'#FFFFFF',10);
$thumb -> Createthumb($source);
You can look at https://www.phpcontext.com/thumbnailer/ . There's a script for creating nice rounded corner thumbs with PHP. They are antialiased too.
Instead of modifying the image, why not just wrap it in some HTML that has images at each corner that overlay the original to provide the appearance of rounded corners?
By doing the image editing in your .php script, you're going to put undue load on your web server, and that means your application won't scale well.
GD is great for image manipulation, but it would be much easier for you and much easier on your server if you used CSS.
Here's a great tutorial for some cool image effects using CSS:
http://www.webdesignerwall.com/tutorials/css-decorative-gallery/
For modern browsers, you can do it in pure CSS:
http://www.css3.info/preview/rounded-border/
A couple of other noteworthy ones:
http://www.spiffycorners.com/
http://www.html.it/articoli/niftycube/index.html
its easy to create some rounded thumbs using php, just use Thumbnailer :)
There are a lot of options available, you can find them using Google. The easiest way though is using the Thumbnailer. It's as simple as two lines of code:
// make an object
$th=new Thumbnailer("your-photo.jpg");
// create a 120x90 thumb and round its corners
$th->thumbFixed(120,90)->round()->save("your-thumb.jpg");
Fun it is, isn't it? :) There are a lot of other options available. The corners will be antialiased.
It seems most or all of the libraries referenced in the other answers here are now dead and gone for one reason or other.
After some exploring, I settled on claviska/SimpleImage as a good library for rounded rectangles (and lots of other handy stuff!)

Categories