...

View Full Version : image resizing



blommethomas
02-03-2007, 02:46 PM
I would like to have a program that changes my images to a size specially made for a specified screen size. i don't wan't to have one image for every screensize, because it takes too much space on the server and I don't wan't to adjust one image for 20 other ones.

Therefore I was thinking of resizing the images of php, which I know how I should do it, but I don't know how I can output it to a specific place in my html.

Can I output this resized image to a table cell for example and make an image map of it?

mlseim
02-03-2007, 03:54 PM
I can help with the resize and placement part ...

You have your HTML file with something like this:

<table>
<tr>
<td><img src="resize.php?photo=myhouse.jpg"></td>
</tr>
</table>


Then, you have a PHP script called "resize.php" ...


<?php
// The file
$filename = $_REQUEST['photo'];
$wid=135;
$hei=135;

// Content type
header('Content-type: image/jpeg');
header('Content-length: '.filesize($filename));

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $wid;
$new_height = $hei;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>



Wherever you put this: <img src="resize.php?photo=myhouse.jpg">
The image will appear. This size is set within the script, but you could
create two new variables for the "width" and "height" and send them
to the script.
<img src="resize.php?photo=myhouse.jpg&w=150&h=200">

blommethomas
02-07-2007, 08:56 PM
this seems to return a number instead of a real image,
see blommethomas.downfire.com for details

mlseim
02-07-2007, 10:10 PM
I see one problem ...

The PHP script is using .jpg not .png type images.

Also ... I'm not sure about these lines:

<META http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="date" content="2007-02-04T21:03:12+0100">
<meta http-equiv="Content-Script-Type" content="text/javascript">

What is that red line all about?
You already describe the content type two lines above it.
.... and this is odd: http-equiv="Content-Script-Type"

blommethomas
02-08-2007, 08:37 PM
the Content-Script-Type line was automatically generated and it should normally give no problems. I need javascript for further manipulation of the webpage

main.php:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Homepage of Thomas Blomme</TITLE>
<META name="Author" lang="en" content="Thomas Blomme">
<META name="keywords" lang="en" content="homepage, java, netbeans, JDance,linux, gentoo, mysql, php">
<META http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="date" content="2007-02-04T21:03:12+0100">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</HEAD>
<BODY>
<?php
//initialisatie van enkele benodigde variabelen
include("properties.inc");
$multiplier = $width . 0.90/$menubarWidth;
?>

<TABLE width="100%" border="0" align="center">
<TR>
<TD colspan="2">
<?php
echo "<img src='functions/resize.php?picture=menubar_" + $lang + ".png" +
"&multiplier=" + $multiplier + "&imageWidth=" + $menubarWidth +
"&imageHeigth=" + $menubarHeigth +
"' usemap='menubar' border='0'></img>";
?>

</TD>
</TR>
</TABLE>

</BODY>
</HTML>


properties.inc:


<?php
$menubarWidth = 1000;
$menubarHeigth = 50;
$numberOfMenubarLinks = 8;
$menubarItem = array (1 => "home", "biography", "music", "pictures", "linux",
"games", "java", "links");
$nlAltMenubarItem =array (1 => "start", "biografie", "muziek", "foto's", "linux",
"spelletjes", "java", "links");

$bannerWidth = 1000;
$bannerHeigth = 50;
$numberOfBannerLinks = 6;
$bannerItem = array (1 => "apache", "mysql", "php", "java", "netbeans", "gentoo");

?>


functions/resize.php


<?php
//parameters: picture, multiplier, imageWidth, imageHeigth
include(../properties.inc");

$width = $imageWidth . $multiplier;
$heigth = $imageHeigth . $multiplier;

header('Content-type: image/jpeg');
header('Content-length: '.filesize($picture));

$image = imageCreateFromPNG($picture);
imageCopyResized($pictureResized, $picture, 0, 0, 0, 0, $width, $height,
$imageWidth, $imageHeight);

imagePNG($pictureResized, null, 100);
?>

mlseim
02-08-2007, 10:20 PM
I'm just stabbing at straws now ...

How about this line?

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

should that be:

header('Content-type: image/png');

... more info ...
http://www.fileformat.info/info/mimetype/image/png/index.htm

=======================

Another thought ...

Make a complete duplicate of your code in another directory and
this time, use all JPEG images, and code (no PNG stuff). See if it
works with JPEG. Perhaps this is just a PNG issue.


.

Len Whistler
02-09-2007, 12:22 AM
Make a complete duplicate of your code in another directory and
this time, use all JPEG images, and code (no PNG stuff). See if it
works with JPEG. Perhaps this is just a PNG issue.

Great code and something I was looking for to generate thumbnails. The code from mlseim's first post works perfectly with jpeg's.

mlseim
02-09-2007, 01:07 AM
I originally used this script when I helped someone
out with a small --> full-size view ... (magnifying glass)

Here's the experimental site we were working on:

http://www.catpin.com/grillz

again, it's all JPEG (not PNG)

blommethomas
02-12-2007, 07:47 PM
I'll give jpeg a try

blommethomas
02-13-2007, 08:30 PM
the photo variable you give to the function is relative to the path of the new function and not to that of the page who calls the function, if my logic is right. Can someone confirm that?

blommethomas
02-13-2007, 09:08 PM
Even the most basic script doesn't work, I have copy/pasted the above and I'm using jpeg now:

blommethomas.downfire.com/test.html:


<table>
<tr>
<td><img src="functions/resize.php?photo=language_nl.jpeg"></td>
</tr>
</table>


blommethomas.downfire.com/functions/resize.php:


<?php
// The file
$filename = $_REQUEST['photo'];
$wid=135;
$hei=135;

// Content type
header('Content-type: image/jpeg');
header('Content-length: '.filesize($filename));

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $wid;
$new_height = $hei;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>


The image is both in the root directory and in the functions directory


When accessing the function directly:
<font face=verdana color=000000 ><br />
<b>Fatal error</b>: Call to undefined function: imagecreatetruecolor() in <b>/www/users/blommethomas.downfire.com/functions/resize.php</b> on line <b>17</b><br />
</font>

mlseim
02-14-2007, 01:17 AM
Maybe there's something wrong or not installed with your GD Library?

blommethomas
02-14-2007, 06:30 AM
you mean of the server I'm using?

Is this a functions specific to php5, as it might be possible my server uses php4

And is this function really necessary to perform the script?

CFMaBiSmAd
02-14-2007, 07:38 AM
PHP by itself is only a programming language. To manipulate images it relies on an external library of functions. The imagecreatetruecolor() function - http://www.php.net/manual/en/function.imagecreatetruecolor.php - as it states in the PHP manual section for that function, is part of the GD library - http://www.php.net/manual/en/ref.image.php. The GD library must be present and enabled within your PHP installation in order to use the functions it provides, as indicated in the error message you received - "Fatal error: Call to undefined function: imagecreatetruecolor()"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum