Resize content of HTML image markup with PHP - php

My HTML page displays an image with the following code (using Flickr)
<?php
echo '<img src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">';
?>
The thing is that I'd like to resize this image to then display it on the page and I don't know how to do that.

Often sites such as flickr allow you to request different sized versions of the image so your best bet is likely to be to request an appropriately sized image.
I'm not sure of the exact details but I looked at http://www.flickr.com/photos/29609591#N08/5735893153/sizes/z/in/photostream/ (a photo I found on the front page of the site) and there are links to different sizes. They seem to be in the same form as yours with the addition of _x just before the .jpg (where x is a letter dependant on size).
I couldn't find any documentation on this in a quick search but in this case we had:
_s for a square
_t for a thumbnail
_m for a small
(no extension) for a medium 500
_z for a medium 640
_b for a large
_o for original - this url seems to be different from the others
I suspect the criteria for these is by fitting to an upper limit of dimensions but I don't know for sure. Trial and error may help you out.

Add width and height attributes to your html string.
<?php
echo '<img width='200px' height='200px' src="http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">';
?>

Related

PHP page doesn't display downloaded PNG files

I am using QRICKIT (qrickit.com) to generate QR codes. Instead of generating codes every time the page refreshes, I want to download them and use the ones already generated.
So, within the loop that iterates through my database table, I do this:
$qrcodebase = "https://qrickit.com/api/qr.php?d=";
$filename = "./product_images/" . $row["itemID"] . "_QR.png";
if (!(file_exists($filename))) {
$url = $qrcodebase . $myurl . $brandpage . "/?itemID=" . $row["itemID"];
file_put_contents($filename, file_get_contents($url));
}
echo "<td><img src=\"" . $filename . "\" height=60 width=60></td>";
When this executes, it generates the right HTML to display the image, and when I FTP to the product_images folder, the QR codes are there. When I download the QR codes and open them up in an image editor, they open just fine. But, when the web page displays, I get an X in a box that indicates a bad image. Does anyone have any idea why?

Display last uploaded image

I working on an image gallery with the thumbnails and main image on the same page. I have ordered the thumbnails so that the last uploaded image standing on top but I also want that this image is displayed as main image. My site is http://www.robcnossen.nl/view_album.php?album_id=7
I can't find if it can be done with mysql but maybe I am looking right.
I'm trying to find the solution in PHP but all what I find on internet I can't get it working for me.
I thought that array multisort, filectime and ksort would solve my problem but nothing changed.
A part of my code is;
print_r ($images);
//$dirname = dirname(__FILE__);
//filectime($dirname);
//ksort($images);
array_multisort($images, SORT_DESC);
if(isset($image['album'], $image['id'], $image['ext']));
$foto = 'uploads/' . $image['album'] . '/' . $image['id'] . '.' . $image['ext']. '';
$standaardwaarde=isset($_GET['image_id']) ? $_GET['image_id'] :$foto;
echo'<img src="' ,htmlentities($standaardwaarde), '" title="" />';
How can the last uploaded image be shown as main image?
If the $images is a return variable from a DB query, you can change that query by adding an ORDER BY timestamp (I see on your var_dump that there is a timestamp).
If you store it in a mysql database and your image are stored with an auto increment field or a date field you can use the ORDER BY clause to get the last image as the first (inverse order):
ORDER BY id DESC or ORDER BY timestamp DESC.
your main image will be then:
$retrieved_array[0]['field_to_show']

PHP: Obtain image size from an image hosted on S3 / Cloudfront

I have images stored in an S3 bucket, going to a cloudfront distribution aliased to images.mydomain.com.
In my image heavy php pages have the usual image tags (fed from my server, not ec2):
img src="http://images.mydomain.com/picture.jpg" width="XXX" height="XXX"
I'm trying to find an easy and fast way to get the width and height XXX from this image distributed on cloudfront.
1) I have tried getimagesize(). Here's a simplified function showing what I need to do. It works for images stored on my server, but fails on images fed from cloudfront.
function imagetag2($colorurl){
$img_path = getimagesize($colorurl);
$width=$img_path[0];
$height=$img_path[1];
$imagetag='<img src="' . $colorurl . '" width="' . $width . 'px" height="' . $height . 'px">';
return $imagetag;
}
Returned: "failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known ...." This code works fine for images stored on my server.
3) I am familiar with the "Framework for managing images on cloudfront" to actually resize the image to the dimensions I need, but this package seems like overkill for this small problem.

Display thumbnails of original images saved as blobs in the mysql

I saved an original image in my database using the following fields:
file_name VARCHAR(255)
mime_type VARCHAR(255)
file_size INT
file_data LONGBLOB
My PHP code for saving is:
$image = $_FILES['image'];
$info = getImageSize($image['tmp_name']);
$query = "CALL saveImageInDataBase('" .
mysql_real_escape_string(file_get_contents($image['tmp_name'])) . "', '" .
mysql_real_escape_string($image['name']) . "', '" .
mysql_real_escape_string($info['mime']) . "', " . $image['size'] . ")";
$result = mysql_query($query);
I want to create a thumbnails of original images out of above data in the server (I'm using PHP) in order to display on the site (to display a list of links of images).
Can someone tell me the simplest way to do this?
You are doing the whole thing wrong.
DO NOT store images in the database. It makes absolutely no sense in the context of an HTML-drven website.
DO NOT create thumbnails on the fly. Create them right after upload and store in the files along with original image.
For the particular code you may search either google or this site. There are over 100500 codes written already I believe.

imagick convert to ico fails for some files

here I have to cache about 2000 favicon.ico files for performance enhancements. I grab the files and try to shrink them via IMagick (v.6.6.0) and PHP 5.3.5
The PHP code for this is
try {
$image = new Imagick($im_hint . ':' . BASE . '/upload/favicon.ico');
$image->cropThumbnailImage(16, 16);
$image->setImageFormat('ico');
$image->writeImage(BASE . '/favicons/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id . '.ico');
} catch (Exception $e) { die($e->getMessage()); }
where $im_hint could be ico, png, jpg and so on.
For 99% of the files all is fine and I get a working ICO file. But for one percent of files, I get only a blank ICO file and I don't know why? An example for an ICO file where this code fails is http://www.augensound.de/favicon.ico
I tried to comment out the cropThumbnailImage call and try to use setFormat instead of setImageFormat and tried to save it as PNG...but nothing works. There is also no exception.
Regards
Not an answer to your question, but I get a blank image too when I open the example file in IrfanView or PhotoImpact. It's not a multi-page/multi-resolution file so there is nothing to switch. The canvas just is blank.
Windows 7's built-in preview renders it fine, though.
It could be that IM can't deal with these files because they have the wrong format or sub-format.

Categories