Posts tagged Image

Image To HTML Text Converter Class

<?php
/*
* Image to HTML Text Converter Class
* Version 1.5
*
* Copyright 2000 by Markus Dobel (mdobel@kawo2.rwth-aachen.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* Requirements: PHP with GD support for the desired image format
*
* Note: Tested with Netscape Communicator 4.7 (Linux, Windows)
* and MS Internet Explorer 5.0 (Windows)
* The only font-size which seems to work nicely with all of
* them at once is 12px. (with the default monospace font)
*
*
* Functions:
* LoadImage (string filename);
* LoadText (string filename);
* SetText (string text);
* ImageInfo ();
* PrintInfo ();
* PrintStylesheet ();
* PrintImage ();
*
* print a whole page with this function:
* PrintPage (string imagefilename, string textfilename);
*/

class HTMLImage {

/* background and font size */
var $stylesheet=”
<style type=\”text/css\”><!–
BODY {
background-color: #202020;
}
PRE {
font-family: monospace;
font-size: 12px;
}
//–></style>
“;

/* Do not set these variables */
var $image; // Image handle
var $imagetype; // Image type (numerical)
var $imageheight; // Dimensions of image
var $imagewidth;
var $text; // Text to print
var $textlength;

var $imagetypes=array(1=>”GIF”, 2=>”JPEG”, 3=>”PNG”);

function LoadImage($filename) {
/* Load Image, set type and size */
unset($this->imageheight);
unset($this->imagewidth);
unset($this->imagetype);
if (isset($this->image)) { ImageDestroy($this->image); }
unset($this->image);
if(is_readable($filename)) {
list($width,$height,$type)=GetImageSize($filename);
switch ($type) {
case 1:
case 2:
case 3:
if (function_exists(“ImageCreateFrom”.$this->imagetypes[$type])) {
$cleartype = $imagetypes[$type];
eval(“\$this->image=ImageCreateFrom”.$this->imagetypes[$type].”(\”".$filename.”\”);”);
$this->imageheight = $height;
$this->imagewidth = $width;
$this->imagetype = $type;
return(true);
} else {
print(“Warning: This version of PHP has no “.$this->imagetypes[$type].” Support<br>”);
return(false);
}
break;
default:
print(“Warning: Cannot determine image type of “.$filename.”\n”);
return(false);
}
} else {
print(“Warning: Cannot access “.$filename.”. (Does not exist or has wrong permissions)<br>”);
return(false);
}
}

function LoadText($filename) {
/* Load textfile into class, return true on success */
if(is_readable($filename)) {
$fp = fopen($filename,”r”);
$text = fread($fp,filesize($filename));
fclose($fp);
return($this->SetText($text));
} else {
print(“Warning: Cannot access “.$filename.”. (Does not exist or has wrong permissions)<br>”);
return(false);
}
}

function SetText($text) {
/* Set text from a variable, return true on success */
$text=preg_replace(“/\s+/”,”",$text);
$len=strlen($text);
if($len>0) {
$this->text=$text;
return(true);
} else {
unset($this->text);
print(“Warning: Text contains no printable characters<br>”);
return(false);
}
}

function PixColortoHTML($img, $x, $y) {
/* internal function to get the HTML color code for a pixel */
$rgb=imagecolorsforindex($img,imagecolorat($img,(int)$x,(int)$y));
return(sprintf(“#%02x%02x%02x”,$rgb["red"],$rgb["green"],$rgb["blue"]));
}

function ImageInfo() {
if(isset($this->image)){
return($this->imagetypes[$this->imagetype].” Image (“.$this->imagewidth . “x” . $this->imageheight.” pixels)”);
} else {
return(“No image loaded”);
}
}

function PrintInfo() {
print($this->ImageInfo());
}

function PrintStylesheet() {
print($this->stylesheet);
}

function PrintImage() {
/* print image block */
/* check for existance of image and text */

if(isset($this->image) and isset($this->text)) {
/*
* Create rescaled version of the image for text output.
* Rendering the image unscaled in monospace font ends up
* with a very ugly aspect ratio.
*/
$newheight=(int)($this->imageheight * 0.5);
$scaledimg=ImageCreate($this->imagewidth,$newheight);

ImageCopyResized($scaledimg, $this->image,0,0,0,0,$this->imagewidth,$newheight,$this->imagewidth,$this->imageheight);
$pixelcount=$this->imagewidth*$newheight;
$len=strlen($this->text);
$i = 1;

/* Calculation and output of first pixel */
$oldcol=$this->PixColortoHTML($scaledimg,0,0);
print(“<pre><font color=”.$oldcol.”>”);
print(htmlspecialchars($this->text[0]));

/* Calculate and output the remaining image in a loop */
while($i<$pixelcount) {
$curcol=$this->PixColortoHTML($scaledimg,$i%$this->imagewidth,$i/$this->imagewidth);
if ($oldcol!=$curcol) {
print(“</font><font color=”.$curcol.”>”);
}
print(htmlspecialchars($this->text[$i%$len]));
$i++;
if($i%$this->imagewidth==0) {
print(“\n”);
@set_time_limit(1);
}
$oldcol=$curcol;
}
print(“</font></pre>\n”);
ImageDestroy($scaledimg);
}
}

function PrintPage($imgfile,$textfile) {
if($this->LoadImage($imgfile) and $this->LoadText($textfile)) {
print(“<html><head><title>”);
$this->PrintInfo();
print(“</title>”);
$this->PrintStylesheet();
print(“</head><body>”);
$this->PrintImage();
print(“</body></html>”);
}
}
}
?>

Leave a comment »

Dynamic Image Rotation

<script language=”php”>
// =============================================================
// Script: Image Rotation Script
// With Adjustable Timing and Unlimited Images
// Utilizes META REFRESH
// Function: Displays random images continuously in a slideshow
// presentation format
// PHP ver: 3.x, 4.x
// Author: Jarrod Major
// Email: jarrod@nucleus.com
// Copyright: (C)2001 Nucleus Information Service Inc.
// Licence: Free, drop me a line letting me know where you
// used the script. Please keep this header attached.
// =============================================================
// Disclaimer:
// Use of this code is at the programmer’s own risk. Damage
// to any property as a result of the script’s use is the sole
// responsibility of the programmer, no blame shall be laid on
// the author of this script or his employer.
// =============================================================

function rotate_images()
{
// open directory, images must be in a separate directory
$dir_pointer = dir( “pic” );

// make an array of filenames
while( $entryName = $dir_pointer->read() )
{
// don’t include parent/current directory listing /. /..
if( !ereg( “^\.”, $entryName ) )
$file_array[] = $entryName;

// could be done this way to simplify some code below
# $file_array[] = “directory_name/$entryName”;
}

// close directory
$dir_pointer->close();

$count_array = count( $file_array ) – 1;

// intialize random seed
srand( time() );

// get a random index number to pull from array
$random_no = rand( 0, $count_array );

// make a file pointer
$file_name = “directory_name/$file_array[$random_no]“;

// or if directory added to array element
#$file_name = $file_array[$random_no];

// get image dimensions
$image_size = getimagesize( $file_name );

// create dynamic image tag
$out = sprintf( ‘<img src=”%s” %s alt=”image.” />%s’, $file_name, $image_size[3], “\n” );

return $out;
}
</script>
<html>
<head>
<!– to adjust timing change ‘content’ value below –>
<meta http-equiv=”Refresh” content=”3″ />
<title>Test Image shuffler</title>
</head>
<body>
<?php print rotate_images(); ?>
</body>
</html>

Leave a comment »

Random Image

Displays a random image on a web page. Images are selected from a folder of your choice.

<?php

/*
Name your images 1.jpg, 2.jpg etc.

Add this line to your page where you want the images to
appear: <?php include “randomimage.php”; ?>
*/

// Change this to the total number of images in the folder
$total = “11″;

// Change to the type of files to use eg. .jpg or .gif
$file_type = “.jpg”;

// Change to the location of the folder containing the images
$image_folder = “images/random”;

// You do not need to edit below this line

$start = “1″;

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo “<img src=\”$image_folder/$image_name\” alt=\”$image_name\” />”;

?>

Leave a comment »