Image Resize script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Image Resize script

Post by Stelios »

Hi everybody

I am designing a web based database with music and images. The central idea is that people will be able to upload images and songs, which then can be accessed by ppl using a simple search engine. My problem is that the images vary in size. So I have created a script which automatically resizes the image. My problem is how to incorporate the resize script in the display script. Currently the display script which is for the images is:

Code: Select all

<td align="center"><img src="../&#123;$row&#1111;'image_name']&#125;" alt="&#123;$row&#1111;'image_name']&#125;" /></td>
The script I have created for the resizing is called resize_image.php and I figured out the right way would be:

Code: Select all

<td align="center"><img src="resize_image.php=../&#123;$row&#1111;'image_name']&#125;" alt="&#123;$row&#1111;'image_name']&#125;" /></td>
Unfortunately this does not work. Anybody has any idea why is this happening? Plase help....!

Thanks a lot! :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you forget to add the '?' to the resize url? Does your resize script use $_SERVER['QUERY_STRING'] ?
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

Thats what am using...

Code: Select all

$image = $HTTP_GET_VARS&#1111;'image'];
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

k.. the second code block in your post didn't have an ?image=... it just had =...

Is this a mistake in posting, or in the code?
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

most likely in the code...
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

Still cant get it to work...I'll post some of my code...

Code: Select all

<?php #resize_image.php

$image_name = $HTTP_GET_VARS&#1111;'image_name'];

if (!$max_width)
  $max_width = 80;
if (!$max_height)
  $max_height = 60;

$size = GetImageSize($image_name);
$width = $size&#1111;0];
$height = $size&#1111;1];

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if ( ($width <= $max_width) && ($height <= $max_height) ) &#123;
  $tn_width = $width;
  $tn_height = $height;
&#125;
else if (($x_ratio * $height) < $max_height) &#123;
  $tn_height = ceil($x_ratio * $height);
  $tn_width = $max_width;
&#125;
else &#123;
  $tn_width = ceil($y_ratio * $width);
  $tn_height = $max_height;
&#125;

$src = ImageCreateFromJpeg($image_name);
$dst = ImageCreate($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,
    $tn_width,$tn_height,$width,$height);
header('Content-type: image/jpeg');
ImageJpeg($dst, null, -1);
ImageDestroy($src);
ImageDestroy($dst);

?>
This is to upload the image:

Code: Select all

// Check for an image (not required).
	if (is_uploaded_file ($_FILES&#1111;'image']&#1111;'tmp_name'])) &#123;
		if (move_uploaded_file($_FILES&#1111;'image']&#1111;'tmp_name'], "C:\Program Files/Apache Group/Apache/htdocs/fyp/imageuploads/&#123;$_FILES&#1111;'image']&#1111;'name']&#125;")) &#123; // Move the file over.

			echo '<p>The image has been uploaded!</p>';

		&#125; else &#123; // Couldn't move the file over.
			echo '<p><font color="red">The file could not be moved.</font></p>';
			$m = '';
		&#125;
		$m = $_FILES&#1111;'image']&#1111;'name'];
	&#125; else &#123;
		$m = '';
	&#125;
And finally this is to display it:

Code: Select all

<?php
$page_title = 'Search Results';
include_once('header.html');
include_once('mysql_connect.php');


$searchtype=$HTTP_POST_VARS&#1111;'searchtype'];
$searchterm=$HTTP_POST_VARS&#1111;'searchterm'];

$searchterm= trim($searchterm);

if (!isset($_SESSION&#1111;'first_name'])) &#123;
	
	header ("Location:  http://" . $_SERVER&#1111;'HTTP_HOST'] . dirname($_SERVER&#1111;'PHP_SELF']) . "/login.php");
	ob_end_clean(); // Delete the buffer.
	exit(); // Quit the script.
&#125;

  if (!$searchtype || !$searchterm)
  &#123;
     echo 'You have not entered search details.  Please go back and try again.';
     include('footer.html');
	exit;
  &#125;
  
  $searchtype = addslashes($searchtype);
  $searchterm = addslashes($searchterm);

$query = "SELECT * FROM uploads,images WHERE ".$searchtype." like '%".$searchterm."%' AND uploads.song_id = images.image_id ";
$result = mysql_query ($query);
$num_results = mysql_num_rows($result);

echo '<table border="1" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="10%"><b>Song Title</b></td>
<td align="center" width="10%"><b>Music Type</b></td>
<td align="left" width="10%"><b>Song Duration</b></td>
<td align="center" width="40%"><b>Song Review</b></td>
<td align="center" width="10%"><b>Rating</b></td>
<td align="center" width="20%"><b>Image</b></td>
</tr>';



echo '<p>Number of songs found: '.$num_results.'</p>';
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) &#123;

	// Display each record.
	echo "	<tr>
		<td align="center">" . stripslashes($row&#1111;'song_title']) . "</td>
		<td align="center">" . stripslashes($row&#1111;'type']) . "</td>
		<td align="center">" . stripslashes($row&#1111;'duration']) . "</td>
		<td align="center">" . stripslashes($row&#1111;'review']) . "</td>
		<td align="center"><img src="../fyp/&#123;$row&#1111;'rating']&#125;" alt="&#123;$row&#1111;'rating']&#125;" /> .</td>
		<td align="center"><img src="resize_image.php?image_name=&#123;$row&#1111;'image_name']&#125;" alt="&#123;$row&#1111;'image_name']&#125;" /></td>
	</tr>\n";

&#125;

echo '</table>'; // Close the table.
	

include_once('footer.html');
?>
Can you please help???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tested the resize script seperately, and it's working? If you are working with GD2, you may want to use imagecopyresampled() instead of imagecopyresized(). Also, imagecreatetruecolor() is better to use than imagecreate() since you are working with JPEGs



So which part isn't working?
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

am not getting the image back, just getting the red cross...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's possible the -1 passed to the quality value may cause it.. try removing it.
User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

Ive tried everything but it doesnt work. Is there any other way to resize the pictures without having to call a separate script to do it? I mean is there any way to do this on the same script I have created to display the search results?

Thanks again!!!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

That's great but how do I install the GD library?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

User avatar
Stelios
Forum Commoner
Posts: 71
Joined: Fri Feb 06, 2004 6:25 am
Location: Surrey/UK

Post by Stelios »

Did everything, installed the gd but still image doesnt appear. Can you spot a mistake on my code?

That'a a part of my upload script

Code: Select all

// Check for an image (not required).
	if (is_uploaded_file ($_FILES&#1111;'image']&#1111;'tmp_name'])) &#123;
		if (move_uploaded_file($_FILES&#1111;'image']&#1111;'tmp_name'], "C:\Program Files/Apache Group/Apache/htdocs/fyp/imageuploads/&#123;$_FILES&#1111;'image']&#1111;'name']&#125;")) &#123; // Move the file over.

			echo '<p>The image has been uploaded!</p>';

		&#125; else &#123; // Couldn't move the file over.
			echo '<p><font color="red">The file could not be moved.</font></p>';
			$m = '';
		&#125;
		$m = $_FILES&#1111;'image']&#1111;'name'];
	&#125; else &#123;
		$m = '';
	&#125;
thats your script with slightly changed variables

Code: Select all

&#1111;php:1:cf1ee0597c] 
define(MAX_WIDTH, 100); 
define(MAX_HEIGHT, 100); 
$m = $_GET&#1111;'m']; 

$t_width = 50; 
$t_height = 50; 
if(stristr($pic,".jpg")) 
&#123; 
header("Content-type: image/jpeg"); 
$img = imagecreatefromjpeg($m); 
$width = imagesx($img); 
$height = imagesy($img); 
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height); 
if ($scale < 1) 
&#123; 
$new_width = floor($scale*$width); 
$new_height = floor($scale*$height); 
$tmp_img = imagecreatetruecolor($new_width, $new_height); 
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, 
$new_width, $new_height, $width, $height); 
imagedestroy($img); 
$img = $tmp_img; 
imagejpeg($img); 
&#125; 
else 
&#123; 
imagejpeg($img); 
imagedestroy($img); 
&#125; 
&#125; 
else 
&#123; 
$im = imagecreate(100,100); 
$blue = imagecolorallocate($im,0,0,200); 
$red = imagecolorallocate($im,255,0,0); 
imagestring($im,2,2,5,"ERROR",$red); 
imagejpeg($im); 
imagedestroy($im); 
&#125; 
?> 
&#1111;/php:1:cf1ee0597c]
and this is part of the script that is supposed to display the image on a table...

Code: Select all

<td align="center"><img src="resize_image.php?m=../fyp/imageuploads/&#123;$row&#1111;'image_name']&#125;" alt="&#123;$row&#1111;'image_name']&#125;" /></td>
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

#1, check to see if the file exists.

#2, try this :

Code: Select all

echo '<img src=resizeimage.php?m=http://www.yoursite.com/fyp/imageuploads/'.$row&#1111;'image_name'].' alt="'.$row&#1111;'image_name'].'">';
instead of

m=../fyp/imageuploads/{$row['image_name']}\

the reason is because i'm not sure if you have your php scripts in another directory (meaning this may be why you are telling the src to look in a different subdirectory in the root) or what...

other than that, do you have a link in which we can look to see the script in action? if I can view the source on the site itself, i can probably point out where you are going wrong. Simply because it sounds like the destination you are pointing for the image seems to be wrong...
Post Reply