Jump to content

[SOLVED] Thumbnail on the fly...


dennismonsewicz

Recommended Posts

I am trying to create a thumbnail of an image on the fly, but its not working.

 

Here is my code:

 

 // load image and get image size
	  $img = imagecreatefromjpeg( "{$url}" );
	  $width = imagesx( $img );
	  $height = imagesy( $img );

	  // calculate thumbnail size
	  $new_width = $thumbWidth;
	  $new_height = floor( $height * ( $thumbWidth / $width ) );

		header("Content-Type: image/jpeg");

	  // create a new temporary image
	  $tmp_img = imagejpeg( $new_width, $new_height );

 

 

This is my code to display the image:

 

<img src="<?php echo $tmp_img; ?>" border="0" />

 

Any help?

Link to comment
Share on other sites

Use this function and put it in it's own file. say imageresize.php

 

<?php
/**********************************
*  Will resize an image to a      *
*  max width or height and keep   *
*  aspect ratio Name this file    *
*  anything you like.             *
*  I will use imageresize.php     *
**********************************/

header('Content-type: image/jpeg');
function resampleimage($maxsize, $sourcefile, $imgcomp=0)
   {
   $g_imgcomp=100-$imgcomp;
   if(file_exists($sourcefile))
       {
       $g_is=getimagesize($sourcefile);
       if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
           $new_width=$g_is[0];
           $new_height=$g_is[1];
       } else {
       $w_adjust = ($maxsize / $g_is[0]);
       $h_adjust = ($maxsize / $g_is[1]);
        if($w_adjust <= $h_adjust)
        {
        $new_width=($g_is[0]*$w_adjust);
        $new_height=($g_is[1]*$w_adjust);
        }
        else
        {
        $new_width=($g_is[0]*$h_adjust);
        $new_height=($g_is[1]*$h_adjust);
        }
       }
       	//SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
    $image_type = strrchr($sourcefile, ".");

    //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
	switch($image_type) {
		case '.jpg':
			$img_src = imagecreatefromjpeg($sourcefile);
			break;
		case '.png':
			$img_src = imagecreatefrompng($sourcefile);
			break;
		case '.gif':
			$img_src = imagecreatefromgif($sourcefile);
			break;
		default:
			echo("Error Invalid Image Type");
			die;
			break;
		}
       $img_dst=imagecreatetruecolor($new_width,$new_height);
       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
       imagejpeg($img_dst);
       imagedestroy($img_dst);
       imagedestroy($img_src);
       return true;
       }
       else
       return false;
   }
// now call the image from any other page with <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 />
resampleimage($_GET['maxsize'], $_GET['source']);
?>

 

now call it like

 

<img src="imageresize.php?maxsize=xxx&source=<? echo $tmp_img; ?>" border="0" />

 

Ray

 

 

Link to comment
Share on other sites

ok disregard what I just asked...

 

I tried your script and got nothing:

 

<?php
$imagename = $result['name'];

$imagesize = $result['size'];

$kbsize = number_format($imagesize / 1024);

$url = $result['url'];

$uploadedby = $result['username'];

$cats = $result['categories'];

$numberofdownloads = $result['number_of_downloads'];

?>
            
            <div class="maincontent">
            	<div class="maincontentheader">
                	<h2><?php echo ucwords($username); ?> here is <?php echo $imagename; ?>'s information!</h2>
                </div>
                
                	<table width="690" cellpadding="4" cellspacing="0" border="0" align="center">
                    	<tr>
                        	<td bgcolor="#dcdcdc" width="325" align="center" style="border: 1px solid #666666;"><img src="imageresize.php?maxsize=150&source=<? echo $imagename; ?>" border="0" /></td>
                            <td valign="top"><p><b>Image Name:</b> <?php echo $imagename; ?></p>
                            	<p><b>Image Size:</b> <?php echo $kbsize; ?> KB</p>
                                <p><b>Uploaded By:</b> <?php echo ucwords($uploadedby) ?></p>
                                <p><b>Categories:</b> <?php echo $cats ?></p>
                                <p><b>Number of Downloads:</b> <?php echo $numberofdownloads ?></p>
                            </td>
                        </tr>
                    </table>
                    
                <div class="maincontentfooter"> </div>
            </div>
            
<?php 
  
  	include "includes/footer.php";

?>

Link to comment
Share on other sites

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.