Jump to content

Recommended Posts

Can someone tell me why this isn't working? i am trying to resize an image that gets uploaded to a dir. and db stores the file name under the "file" column. this is the page that displays the image :

 

				 		print '<img src="images/resize_image.php?file=$pic" title="" border="0" width="170" height="135" class="floatright" style="margin-left: 5px; border: 2px solid #CCCCCC; padding: 3px; background: #9bbdfd;" />'; 

 

this is the page that resizes the images:

<?php 
$pic = $HTTP_GET_VARS['file'];
print $pic;
print "image ";

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

$size = GetpicSize($pic);
$width = $size[0];
$height = $size[1];

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

if ( ($width <= $max_width) && ($height <= $max_height) ) {
	$tn_width = $width;
	$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
	$tn_height = ceil($x_ratio * $height);
	$tn_widht = $max_width;
}
else {
	$tn_width = ceil($y_ratio * $width);
	$tn_height = $max_height;
}

$src = ImageCreateFromJpeg($pic);
$dst = ImageCreate($tn_width, $tn_height);
ImageCopyResize($dt, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height);

header('Content-type: image/jpeg');
ImageJpeg($dst, null, -1);
ImageDestroy($src);
ImageDestroy($dst);

?>

Link to comment
https://forums.phpfreaks.com/topic/184649-resizing-images-from-db/
Share on other sites

2 things I have noticed.

 

1.

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

should be

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

 

and secondly remove the print calls on the top section of the script.

Ok. without seeing the main page and where the file name is coming from I cant give any more insight but I did notice 1 other thing that I believe is your main issue..

 

print '<img src="images/resize_image.php?file=$pic" title="" border="0" width="170" height="135" class="floatright" style="margin-left: 5px; border: 2px solid #CCCCCC; padding: 3px; background: #9bbdfd;" />'; 

should be

print '<img src="images/resize_image.php?file='.$pic.'" title="" border="0" width="170" height="135" class="floatright" style="margin-left: 5px; border: 2px solid #CCCCCC; padding: 3px; background: #9bbdfd;" />'; 

 

You cant include variables into a string using single quotes..

here is my main page

 

 

               <?php 
	if(!($db = @ mysql_connect('localhost', 'user', 'pass')))
		{
			print "Error: Could not connect to our database sorry for any inconvenience.<br /> Please try at a later time.";
		}						
		 //select which database you want to edit
		mysql_select_db("db"); 

//			$news_id=mysql_real_escape_string($_GET[news_id]);

		$query = "SELECT * FROM news ORDER BY news_id DESC LIMIT 6";
		$result = mysql_query($query);
	?>
        	<div style="background:#cfdbea; height:180px;padding: 5px; font-size: 14px">
            	<div style="background:#FFFFFF; height:160px; padding:10px;">
         <?php
	 		$i = 1; 

			while ($r=mysql_fetch_array($result))
			{
				$news_id=$r["news_id"];
				$title=$r["title"];
				$fulldesc=$r["fulldesc"];
				$pic=$r["file"];

	?>
        	
	        <div id="slide<?php print $i; ?>" class='slides'>
                <div class='slideTitle' style="padding-bottom:2px;">
                
			<?php 
					if ($pic!="") {
					print "<img src=\"images/resize_image.php?file = '.$pic.'\" border=\"1\" class=\"floatright\" title=\".$pic.\" alt=\"$pic\" />";
//				 		print '<img src="images/'.$pic.'" title="" border="0" width="170" height="135" class="floatright" style="margin-left: 5px; border: 2px solid #CCCCCC; padding: 3px; background: #9bbdfd;" />'; 
}
			 		else 
			 		print '<img src="images/imageholdershow.jpg" title="" border="2" width="170" height="135" class="floatright" style="margin-left: 5px; border: 2px solid #CCCCCC; padding: 3px; background: #9bbdfd;" />'; 
			 ?>
                
			<?php print "<p style='padding: 0px;'><a href='newsevents.php?news_id={$news_id}' style='color:#9F0000; text-decoration:underline'>".$title."</a>"; ?></div>
                <p><?php print substr($fulldesc = $r["fulldesc"], 0, 280); 
	          print "&#8230; <a href='newsevents.php?news_id={$news_id}' style=\"font-size: 11px\">Full Story »</a></p>"; ?>
                 </div>
                 
         <?php
	 		$i++;


			}//end while loop
	?>

Your image tag has to be either:

print "<img src=\"images/resize_image.php?file=$pic\" border=\"1\" class=\"floatright\" title=\".$pic.\" alt=\"$pic\" />";
-- OR --
print '<img src="images/'.$pic.'" title="" border="0" width="170" height="135" class="floatright" style="margin-left: 5px; border: 2px solid #CCCCCC; padding: 3px; background: #9bbdfd;" />'; 

 

Double quotes allow you to have variables in them single quoted strings do not..

eg:

$name = 'Buddski';

echo "Hello, $name, how are you?"; //would output 'Hello, Buddski, how are you?'

echo 'Hello, $name'; // would output 'Hello, $name, how are you?'

echo 'Hello, '.$name.' how are you?'; // would output 'Hello, Buddski, how are you?'

 

After you have executed your script in the browser view the source code.. the page where no image shows up..

Navigate your way through it and find the image tag.. and read it src value..

 

Alternativley you can right click where the image is supposed to be (depending on your browser) and view the properties of the image which will also give you its src value..

it gets the image because it gives me the name of the file. and this is what the source code shows as the image tag path:

 

<img src="images/resize_image.php?file=forbeslogo.jpg" border="1" class="floatright" title="forbeslogo.jpg" alt="forbeslogo.jpg" />

 

and the title and alt attributes in the img tag show the name of the file. but don't know why it is not showing.

Now we know that the issue lies within the resizing script and your main page is working correctly..

 

The best way to see whats going on in the resize script is to goto it in your browser, doing this may show you any errors the script has..

if the resizing script is in the same directory as the file you are passing to it it should work..

 

Are you seeing any errors on the script when you enter it into your browser?

 

for the sake of it. change ImageJpeg($dst, null, -1); to imagejpeg($dst);

 

Also I noticed another issue

 

ImageCopyResize($dt, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height);

 

should be

 

imagecopyresized($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width,$height);

 

We will get there :P

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.