| Home > Computer & I'net > Storage and re-use of image... |
|
|
|||||||||||||||
|
|
|||||||||||||||
|
||||||||||||||||
Storage and re-use of images using PHP/GD - Part II | |
Publishing images on the web is nice, but tedious. It would be nice if we could use a system that automates the uploading, storage, converting and resizing of our images. We could feed that system our images once, and retrieve them later in many different formats. In this article, we will write some scripts that come close to this ideal system. PHP and the GD-library will provide us with an easy method of uploading, searching and publishing our images. (This article is a follow-up on part 1). In this second part, we will write scripts that you can use to retrieve images and search your image database. To be able to use these scripts, you should have read the first part of this article and saved the scripts we wrote there. You also need the image resize script explained in an earlier article. (This was also stated in part 1.) What we will do in this second partWe will write scripts that allow us to:
First, we need a function that, when provided with an image name, does this:
In // define database table containing image info
$img_table = "images";
// connect with database
mysql_connect("yourhost", "youruser", "password");
mysql_select_db("yourdb");
// generates and returns an img-tag
function img_tag($img_file, $attrib) {
global $img_table;
To create an img tag, our function needs the following arguments passed:
Not only will the information in // concatenate the attributes in $attrib
$attribs = "";
foreach ($attrib as $key => $value) {
$attribs .= '+'."$key($value)";
}
Retrieving the image from the database
Now, we have to look in the database and see if our image can be found. We use the provided // retrieve image info from database
$result = mysql_query("SELECT * FROM $img_table ".
"WHERE img_file='$img_file'");
// check that an entry is found
if (mysql_num_rows($result)!=1) {
return false;
}
// get this row
$img_info = mysql_fetch_array($result);
Calculating image height and width
We've got the image information in the We use a slightly modified version of the code we used in the image resize script to calculate the new width and height. // check for maximum width and height
if (isset($attrib["x"])) {
if ($attrib["x"] < $img_info["img_width"]) {
$attrib["w"] = $attrib["x"];
}
}
if (isset($attrib["y"])) {
if ($attrib["y"] < $img_info["img_height"]) {
$attrib["h"] = $attrib["y"];
}
}
// check for need to resize
// convert relative to absolute
if (isset($attrib["w"])) {
if (strstr($attrib["w"], "%")) {
$attrib["w"] = (intval(substr($attrib["w"], 0, -1)) / 100) *
$img_info["img_width"];
}
}
if (isset($attrib["h"])) {
if (strstr($attrib["h"], "%")) {
$attrib["h"] = (intval(substr($attrib["h"], 0, -1)) / 100) *
$img_info["img_height"];
}
}
// resize
if (isset($attrib["w"]) and isset($attrib["h"])) {
$out_w = $attrib["w"];
$out_h = $attrib["h"];
} elseif (isset($attrib["w"]) and !isset($attrib["h"])) {
$out_w = $attrib["w"];
$out_h = $img_info["img_height"] * ($attrib["w"] / $img_info["img_width"]);
} elseif (!isset($attrib["w"]) and isset($attrib["h"])) {
$out_w = $img_info["img_width"] * ($attrib["h"] / $img_info["img_height"]);
$out_h = $attrib["h"];
} else {
$out_w = $img_info["img_width"];
$out_h = $img_info["img_height"];
}
The img tag
The last thing our function has to do, is actually create the img tag and return that. // create and return the img-tag
$img = '<img src="img.php?f('.$img_file.')'.$attribs.'" '.
'width="'.$out_w.'" height="'.$out_h.'" '.
'alt="'.$img_info["img_alt"].'" border="0">';
return $img;
}
The search form
To search the images, we start with a simple form. We can search by title, description, alt-text and image size. Save the form as, for example, <form enctype="multipart/form-data" method="post" action="search.php"> <b>Search image</b><br> Title: <input name="title" type="text"><br> Description: <input name="descr" type="text"><br> Alt-text: <input name="alt" type="text"><br> Width: <select name="width_expr"> <option value="=">=</option>The search script The input from the search form is sent to The first step is the inclusion of the img tag-script. Normally, you would also have to connect to the database. We already did that in the included script, so that's not necessary here. include("imgtag.php");
The query
Using the data posted by the form, we can now build a search query. (We only search for width, height and bytes if a value is given.) // build query
$query = "SELECT * FROM images WHERE ";
// title
$query .= "img_title LIKE '%".$HTTP_POST_VARS["title"]."%' AND ";
// description
$query .= "img_descr LIKE '%".$HTTP_POST_VARS["descr"]."%' AND ";
// alt
$query .= "img_alt LIKE '%".$HTTP_POST_VARS["alt"]."%' AND ";
// width
if (trim($HTTP_POST_VARS["width"])!="") {
$query .= "img_width".$HTTP_POST_VARS["width_expr"].
$HTTP_POST_VARS["width"]." AND ";
}
// height
if (trim($HTTP_POST_VARS["height"])!="") {
$query .= "img_height".$HTTP_POST_VARS["height_expr"].
$HTTP_POST_VARS["height"]." AND ";
}
// bytes
if (trim($HTTP_POST_VARS["bytes"])!="") {
$query .= "img_bytes".$HTTP_POST_VARS["bytes_expr"].
$HTTP_POST_VARS["bytes"]." AND ";
}
// type
$query .= "img_alt LIKE '%".$HTTP_POST_VARS["type"]."%' AND ";
// finish the query (we ended every part with AND)
$query .= "1";
Execute and count the results: // execute the query $result = mysql_query($query); // get the number of results $num_rows = mysql_num_rows($result);The results We start with a basic html-page. We also include a JavaScript function (borrowed from www.vpro.nl) to display the popup with a larger version of the image. ?><html>
<script language="javascript">
/* source: www.vpro.nl */
function bigImage(name) {
if (navigator.appName=="Netscape") {
var imagewindow = window.open('bigimage.php?f='+name,'imagewindow'+name,
Per image, we display one table row. We add a popup link to // one row for each result
for ($i=0; $i<$num_rows; $i++) {
// fetch row
$img_info = mysql_fetch_array($result);
// start table row
echo '<tr><td><a href="bigimage.php?f='.$img_info["img_file"].'" '.
'target="_new" onclick="bigImage('."'".$img_info["img_file"]."')".
'; return false;">';
// generate the image tag
echo img_tag($img_info["img_file"], array("x"=>"250"));
// echo image information
echo "</a></td><td><b>".$img_info["img_title"]."</b><br>".
$img_info["img_descr"]."<hr><b>Alt:</b> ".$img_info["img_alt"].
"<br><b>Pixels:</b> ".$img_info["img_width"]."x".
$img_info["img_height"]."<br><b>Bytes:</b> ".
$img_info["img_bytes"]."<br><b>Type:</b> ".
$img_info["img_type"]."</td></tr>\n";
}
Now just close the table, body and html tags and your search script is finished. Bigimage.phpAs said, this script isn't very complicated. It uses the <html>
<body>
<?php
include("imgtag.php");
echo '<a href="img.php?f('.$HTTP_GET_VARS["f"].')">';
echo img_tag($HTTP_GET_VARS["f"], array("x"=>"400"));
echo '</a>';
?>
</body>
</html>
What we did in part 2
In this last instalment, we wrote an easy-to-use function that generates img tags based on database data. We also made a search script to find the images in the database. In the last section of the article, we tried writing a real script using our newly created function. If you're finished with these two parts, you've now got the following files and directories:
You can also download the complete directory structure as a tar.gz (102 kB). ----------------- Gijs is a full time Dutch student in economics and a spare time Web developer. He spends his time developing scripts using PHP, MySQL and other external programs. Visit his site at: http://gvtulder.f2o.org/ | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·Linux·MySQL·Operating System·Perl / CGIPHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|