...

View Full Version : how i can insert a resized image into HTML file?



kamkam
08-29-2009, 11:15 AM
Hi;

How i can insert an resized image into a html file, please.

If i do like the following, it does not work. Could any one help, please.


<?php
function resizeImage($originalImage,$toWidth,$toHeight, $maintain=true){
//check that image exists
if (!is_file($originalImage) || !is_readable($originalImage)) return false;
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
if ($maintain) list($newWidth, $newHeight) = ( (($toWidth/$toHeight) > ($width/$height)) ?array (intval(($toWidth * $width/$height)), intval($toHeight)) : array (intval($toWidth), intval(($toHeight * $height/$width))));

// Resize the original image
$imageResized = imagecreatetruecolor($newWidth, $newHeight);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $imageResized;
}



?>

<html>
<body>
I want the image sit under me.
<?php
$image=resizeImage("testme.jpg",200,200,"true"); //this one shows on the browser
imagejpeg($image, null, 20);
?>

</body>
</html>

Zangeel
08-29-2009, 11:25 AM
I don't understand the problem, you said the code does work? Then what's the issue?

kamkam
08-29-2009, 11:42 AM
Sorry, the code does not work.

when i open the website, it come out garbage and without the image and some text.

Fou-Lu
08-29-2009, 12:01 PM
Image data cannot be combined with html. I believe that FF will actually take a source of base64 encoded data, but I wouldn't recommend that anyway.

I haven't gone over this code, but at a glance it looks good. Its the html you'll need to remove.
What you want is this file to serve as the image. So you will send an appropriate header indicating thats its an image. This case its a jpg image, if you want to dynamically choose you can retrieve the type with getimagesize on the original image (note that as long as you use imagejpeg it will be a jpeg).
Than you call it as the source for the image in the html, not embedded with the html. Does that make sense?

kamkam
08-29-2009, 12:45 PM
ok, after i removed the html off, i can see the image.

But the problem for me how i can use the resized image to sit into a html file, like the following page:

http://www.realestate.com.au/cgi-bin/rsearch?a=s&cu=fn-rea&s=nsw&ss=&ag=&t=res&snf=rbs&chk=0&lead=&ty=&searchFormSource=advanced+search&tb=+ABBOTSFORD%2C+&u=ABBOTSFORD&is=1&pm=&px=&pme=any&pxe=any&cat=&o=def

http://www.realestate.com.au/cgi-bin/rsearch?a=o&id=105963943&f=0&p=10&t=res&ty=&fmt=&header=&cc=&c=22577237&s=nsw&snf=rbs&tm=1251545919


After i removed the html, i can see the image.


<?php
header('Content-type:image/jpeg');

function resizeImage($originalImage,$toWidth,$toHeight, $maintain=true){
//check that image exists
if (!is_file($originalImage) || !is_readable($originalImage)) return false;
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
if ($maintain) list($newWidth, $newHeight) = ( (($toWidth/$toHeight) > ($width/$height)) ?array (intval(($toWidth * $width/$height)), intval($toHeight)) : array (intval($toWidth), intval(($toHeight * $height/$width))));

// Resize the original image
$imageResized = imagecreatetruecolor($newWidth, $newHeight);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $imageResized;
}



?>


<?php
$image=resizeImage("testme.jpg",200,200,"true"); //this one shows on the browser
imagejpeg($image, null, 20);
?>

kamkam
08-29-2009, 01:57 PM
Thanks, i have known how to do it already.

I saved the above file as testimage.php, then in a html file, i will use the image as following:

<img src='testimage.php?image=image.jpg' />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum