How Do I Resize An Image On The Fly?

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

How Do I Resize An Image On The Fly?

sonoftheclayr
I was wondering how I would go about resizing an image from a databse without saving it to a file.

CODE

<?
include("settings.inc.php");

$get_a=$_GET['a'];
$get_b=$_GET['b'];
$get_width=$_GET['width'];
$get_height=$_GET['height'];


$query="SELECT * FROM $get_a WHERE id='$get_b'";
$result=mysql_query($query);

$image=mysql_result($result,0,"coverimg");
$size=mysql_result($result,0,"imagesize");
$type=mysql_result($result,0,"imagetype");

header("Content-length: $size");
header("Content-type: $type");

echo $image;

?>


Obviously the image I want to resize is $image.
The page name would go something like this: ?a=books&c=1&width=200&height=300.

I would like to resize the image without saving it to file using the variables $get_width and $get_height as the width and height of the resized image.

Does anybody have any ideas that might help?

 

 

 


Reply

vujsa
QUOTE(sonoftheclayr @ Jul 1 2006, 02:22 AM) *
I was wondering how I would go about resizing an image from a databse without saving it to a file.

CODE
<?
include("settings.inc.php");

$get_a=$_GET['a'];
$get_b=$_GET['b'];
$get_width=$_GET['width'];
$get_height=$_GET['height'];
$query="SELECT * FROM $get_a WHERE id='$get_b'";
$result=mysql_query($query);

$image=mysql_result($result,0,"coverimg");
$size=mysql_result($result,0,"imagesize");
$type=mysql_result($result,0,"imagetype");

header("Content-length: $size");
header("(anti-spam-content-type:) $type");

echo $image;

?>


Obviously the image I want to resize is $image.
The page name would go something like this: ?a=books&c=1&width=200&height=300.

I would like to resize the image without saving it to file using the variables $get_width and $get_height as the width and height of the resized image.

Does anybody have any ideas that might help?


Well, the example from PHP.net is as good as I can explain so here it is:
QUOTE(php.net @ Jul 1 2006, 02:49 PM)
Example 1. Resizing an image

This example will display the image at half size.
CODE
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

The image will be output at half size, though better quality could be obtained using imagecopyresampled().


You'll need to be sure to use the right image create function for the image type being used.
Change imagecreatefromjpeg to the correct image format. This will probably require some kind of conditional statement or switch.

This process simply makes the image 1/4 the size it was originally. The example says that the image is half size but it is half as tall AND half as wide. So half of half is a quarter.

You probably want to have your thumbnails all the same size so a percentage calculation won't really work. You also won't want you images to lose their ratios where they may get squished or stretched. You'll need to base the image size on the original dimensions of the image.
First you'll need to get the height and width of the original image.
imagesx() to get the width.
imagesy() to get the height.

CODE
$orig_width = imagesx($source);
$orig_height = imagesy($source);


Then you need to do some calculations. Let's say that you want your thumbnails to be 100px tall.

CODE
$orig_width = imagesx($source);
$orig_height = imagesy($source);

First we figure out the reduction percentage.
$reduction_percentage = $get_width / $orig_width;

$newwidth = $orig_width * $reduction_percentage;
$newheight =  = $orig_height * $reduction_percentage;


Now if your original image was 600W X 800H and you asked for 100px wide then the thumbnail would be:
100W X 133H.
1200W X 1600H = 100W X 133H
500W X 600H = 100W X 120H
300W X 350H = 100W X 116H
700W X 800H = 100W X 114H

But what if you has a photo in the landscape orientation?
1600W X 1200H = 100W X 75H
500W X 400H = 100W X 80H
700W X 300H = 100W X 43H
900W X 600H = 100W X 67H

This may leave you with images that are too short and as a result too small overall!

You may want to add an option to check to see which dimension is larger, the height or width. Then base the thumbnail on on the larger measurement. Maybe use a minimum height calculation. This way your thumbnails will be more uniform and easier to view by the user.

I am concerned about one thing though. You said that you don't want to save the image. If you will be generating many resized images, then your server performance will be impacted. Consider how long it takes you computer to resize an image in PhotoShop or any other graphics editor. Imagine if you did the possibly a thousand times a minute. You computer would be unbelievable slow. The same with the server and if you are sharing a server with other accounts as you probably are, then you may get booted off by your service provider because you keep tying up or crashing the server. Your site may not work at all if you get too many visitors trying to view resized images all at once.
The better method is to save the thumbnail on the server. They don't take much storage space because they are so small. That's the whole point. You will use more storage over all but it will give you much better performance in the end. Bandwidth usage will be the same with either method!

You don't have to go to the trouble of thumbnailing everything manually.
Just have your script check to see if that image has a thumbnail. If it does, then load that image otherwise, create a new thumbnail, save it and then load it.

I use this method to save server resources myself but with a delay system. I'm working on a dynamic signature image generator that is near real-time. It has a buffer added to it so that the image is only refreshed every 20 minutes and then only if it is requested. The signature displays game statistics which don't need to be updated in real-time.

Just remember, servers are very good at serving files like image files. They can do other processes but they can server faster than create.

Hope This Helps. cool.gif

vujsa

 

 

 


Reply

TavoxPeru
QUOTE(sonoftheclayr @ Jul 1 2006, 01:22 AM) *

I was wondering how I would go about resizing an image from a databse without saving it to a file.

CODE

<?
include("settings.inc.php");

$get_a=$_GET['a'];
$get_b=$_GET['b'];
$get_width=$_GET['width'];
$get_height=$_GET['height'];
$query="SELECT * FROM $get_a WHERE id='$get_b'";
$result=mysql_query($query);

$image=mysql_result($result,0,"coverimg");
$size=mysql_result($result,0,"imagesize");
$type=mysql_result($result,0,"imagetype");

header("Content-length: $size");
header("Content-type: $type");

echo $image;

?>


Obviously the image I want to resize is $image.
The page name would go something like this: ?a=books&c=1&width=200&height=300.

I would like to resize the image without saving it to file using the variables $get_width and $get_height as the width and height of the resized image.

Does anybody have any ideas that might help?

Hi I use the following code to resize images of any size to the size that i need.
CODE
<?php
//$width: image width
//$height: image height
//$target: target image width or height
function imageResize($width, $height, $target) {
if ($width > $height) {
    $percentage = ($target / $width);
} else {
    $percentage = ($target / $height);
}

$width = round($width * $percentage);
$height = round($height * $percentage);
return " width=\"$width\" height=\"$height\"";
}

// source image
$photo="images/anyphoto.jpg";

// populate attributes obtained from the image  
$PropImg = array(0,0,0,"");
$PropImg = GetImageSize($photo);
if (isset($PropImg[0])) $nW = $PropImg[0];
else $nW = 100;
if (isset($PropImg[1])) $nH = $PropImg[1];
else $nH = 100;

// generate html img tag
$Img="<img border='0' hspace='3' align='left'";
$Img.=" src=\"" . $photo . "\""  . imageResize($nW,$nH, 220) . ">";

// show the img
echo $Img;
?>


Very simple but function very well smile.gif

Best regards,

Reply

TavoxPeru
QUOTE(sonoftheclayr @ Jul 1 2006, 01:22 AM) *

I was wondering how I would go about resizing an image from a databse without saving it to a file.

CODE

<?
include("settings.inc.php");

$get_a=$_GET['a'];
$get_b=$_GET['b'];
$get_width=$_GET['width'];
$get_height=$_GET['height'];
$query="SELECT * FROM $get_a WHERE id='$get_b'";
$result=mysql_query($query);

$image=mysql_result($result,0,"coverimg");
$size=mysql_result($result,0,"imagesize");
$type=mysql_result($result,0,"imagetype");

header("Content-length: $size");
header("Content-type: $type");

echo $image;

?>


Obviously the image I want to resize is $image.
The page name would go something like this: ?a=books&c=1&width=200&height=300.

I would like to resize the image without saving it to file using the variables $get_width and $get_height as the width and height of the resized image.

Does anybody have any ideas that might help?


Another way to do this is by setting the image width and height as 100 percent inside the <img> tag. The browser then resizes the image according to the window's size, and with Javascript by using document.body.clientWidth and document.body.clientHeight that returns the width and height of the active window, and one function for the On_Load event which sets the image width and height and other to the onresize event which refresh the page.

BTW, i dont test this with Firefox but i guess it will work.

Best regards,

Reply

jagoan
QUOTE(sonoftheclayr @ Jul 1 2006, 01:22 PM) *

I would like to resize the image without saving it to file using the variables $get_width and $get_height as the width and height of the resized image.


this is the script that I use to resize image..

CODE
<?php
$a = "images/".$_GET['src'];
$b = $_GET['w'];

$ext = explode(".",$_GET['src']);
$ext = end($ext);
$ext = strtolower($ext);

if ($ext == "jpg") { header ("Content-type: image/jpeg"); }
else if ($ext == "gif") { header ("Content-type: image/gif"); }
else if ($ext == "png") { header ("Content-type: image/png"); }

if ($ext == "jpg") { $img_src=imagecreatefromjpeg($a); }
else if ($ext == "gif") { $img_src=imagecreatefromgif($a); }
else if ($ext == "png") { $img_src=imagecreatefrompng($a); }

$size = getimagesize($a);
if ($b >= $size[0]) $b = $size[0];
$c = round($b * $size[1] / $size[0]);

$img_dst=imagecreatetruecolor($b,$c);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $b, $c, $size[0], $size[1]);

if ($ext == "jpg") { imagejpeg($img_dst, "", 100); }
else if ($ext == "gif") { imagegif($img_dst); }
else if ($ext == "png") { imagepng($img_dst); }

imagedestroy($img_dst);
?>


using my script, the image will show with ?src=image_name&w=image_width
the images are store in /images directory..

this is the example:
real image
resized image

Reply

TavoxPeru
QUOTE(jagoan @ Aug 29 2006, 03:53 AM) *

this is the script that I use to resize image..

CODE
<?php
$a = "images/".$_GET['src'];
$b = $_GET['w'];

$ext = explode(".",$_GET['src']);
$ext = end($ext);
$ext = strtolower($ext);

if ($ext == "jpg") { header ("Content-type: image/jpeg"); }
else if ($ext == "gif") { header ("Content-type: image/gif"); }
else if ($ext == "png") { header ("Content-type: image/png"); }

if ($ext == "jpg") { $img_src=imagecreatefromjpeg($a); }
else if ($ext == "gif") { $img_src=imagecreatefromgif($a); }
else if ($ext == "png") { $img_src=imagecreatefrompng($a); }

$size = getimagesize($a);
if ($b >= $size[0]) $b = $size[0];
$c = round($b * $size[1] / $size[0]);

$img_dst=imagecreatetruecolor($b,$c);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $b, $c, $size[0], $size[1]);

if ($ext == "jpg") { imagejpeg($img_dst, "", 100); }
else if ($ext == "gif") { imagegif($img_dst); }
else if ($ext == "png") { imagepng($img_dst); }

imagedestroy($img_dst);
?>


using my script, the image will show with ?src=image_name&w=image_width
the images are store in /images directory..

this is the example:
real image
resized image

Well done, but, i take the liberty to optimize your code wink.gif here is my version:
CODE
<?php
$a = "images/".$_GET['src'];
$b = $_GET['w'];

$ext = explode(".",$_GET['src']);
$ext = end($ext);
$ext = strtolower($ext);

$size = getimagesize($a);
if ($b >= $size[0]) $b = $size[0];
$c = round($b * $size[1] / $size[0]);
$img_dst=imagecreatetruecolor($b,$c);

if ($ext == "jpg") {
header ("Content-type: image/jpeg");
$img_src=imagecreatefromjpeg($a);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $b, $c, $size[0], $size[1]);
imagejpeg($img_dst, "", 100);
}
else if ($ext == "gif") {
header ("Content-type: image/gif");
$img_src=imagecreatefromgif($a);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $b, $c, $size[0], $size[1]);
imagegif($img_dst);
}
else if ($ext == "png") {
header ("Content-type: image/png");
$img_src=imagecreatefrompng($a);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $b, $c, $size[0], $size[1]);
imagepng($img_dst);
}
imagedestroy($img_dst);
?>

Best regards,

Reply

CaptainRon
If you want to do things quickly, just use the phpThumb library. Get it from http://phpthumb.sourceforge.net

Reply

Vyoma
I know the article I am going to link here is a little out of context, but I wanted to show this to the community we have here. I am a usual haunt at the 'A List Apart' website, and I found this article.

http://www.alistapart.com/articles/magazinelayout

It deals with automatic resizing functions when there are a set of images. All the images would be auto resized and it would be presented in a nifty magazine like layout. I have not used the scripts, but I found it quite intriguing.

Reply

TavoxPeru
QUOTE(CaptainRon @ Aug 30 2006, 02:01 AM) *

If you want to do things quickly, just use the phpThumb library. Get it from http://phpthumb.sourceforge.net

Yeah, excellent share, never know about this resource, almost because i'm a big fan of sourceforge and always research in it.

best regards,

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. "resizing image" to pop up dimensions - 63.76 hr back. (1)
  2. optimize imagecreatefromjpeg - 192.11 hr back. (1)
  3. mysql_result image/png - 294.39 hr back. (1)
  4. phpthumb resize percent - 366.42 hr back. (1)
Similar Topics

Keywords : resize image

  1. Dynamic Php Image And Better Php Code Question - (10)
  2. Dynamic Gd Image - (2)
    I dont know if someone already made a topic like this, however it's not hard to understand if
    you can php, as many know we need to start a php script with CODE and end with CODE ?>
    to make a image you need a image type, I choos .png because its much cleaner then .jpg CODE
    header("Content-type: image/png"); for the background for the image we need this code CODE
    $image = imagecreatefrompng("http://www.imagefilez.com/out.php/i252132_Userbar.png"); if we wont
    a text in the image we need a font color, we get our font color from HEX values ...
  3. Image Popup On Mouseover - (23)
    I need a script that does this: Lets say I have form with options...you surely know what that is...
    CODE Who do you wanna race?           Derbi Senda 50      Honda NS 50 R      Suzuki ZR
    50      Yamaha DT 50 MX      Aprilia RS 50      - ?         When i put my mouse over
    one of them i want a picture of bike on which your mouse is on to pop up... I hope you understand
    what i mean...i can't rephrase it to be more meaningful...i tried /wink.gif"
    style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" />...
  4. Need Help With Background Image... - (12)
    I want to add background image to all sites on my website but some include "foot.php" and "head.php"
    and some "footer.php" and "header.php" but all include "config.php" but i dont know in which should
    i put it....any help??...
  5. Php :: Image Pixel Per Inch And Conversion - Please Guide (1)
    Hi Masters I want soemone to let me know (maybe through a good tutorial and/or PHP Sample Code)
    the following : 1 - User will only allowed to upload images with minimum 120 and maximum 300 Pixel
    Per Inches 2 - User will only allowed to upload JPG images, however, if Gif / BMB / PNG / ETC is
    uploaded the script will convert that into jpg. 3 - If the Image's PPI is > 300 it will set
    to 300. Please guide . I know this can'y be done by GD it requires ImageMagick, Pleae help
    !!!!!!! ...
  6. Image Works With Php And Gd - (3)
    Hi I have a 1 Big map. What i want is that the user will enter the latitude and longitude. the
    script will show that location in the image. I know how to convert Image X,Y Coordinates into
    Latitide and Longitude and vise versa. But there point is that the image is 1000x1000 wide but i
    want to show only the particular region of that latitude / longitude in 300x300 image. means that
    the PHP script will CROP wht Image (1000x1000) to 300x300. That way, the script will be shoing the
    part of an image, not the whole image. please help !!...
  7. Php :: Adding Image Over Image - (5)
    Hi Masters & Champions. I want some help regarding putting and image over some location (x pixel ,
    y pixel) of an image. Here it is quite important to note that the mage which is Inserted over an
    image must be clickable. Like for example suppose i put T on an image DD then the T must be
    clickable. Please gudie as this is quite important for me . Thanks ...
  8. Script For Viewing A Random Image Needed - (3)
    While browsing the web, I bumped into a script that I could really use, since it's different
    from everything that I saw. Most scripts that view a random image contain a code that chooses an
    image, and then puts the part into the page itself. However, on the www.greenplastic.com web
    site (dedicated to Radiohead), in the top left corner there is a random image module. If you look at
    the code, here's what is used: CODE Now, how can a PHP script be an image, and what
    kind of script that is? If you do visit that URL , you don't get a normal page, b...
  9. How Do I Make PHP Based Image Gallery Like This? - Help Needed (20)
    is it possible to make a page in php, with a url like this:
    httq://www.mysite.com/viewer.php?http://www.mysite.com/galleries/01 (This is a sample link, read
    below) so that in what i change the last part the gallery will change with it? so that i just have
    to make one php-page and this page just shows all the imaes in the map thats in the url after the
    questionmark?? thanks,...
  10. Uploading Image Via Admin Menu? - (2)
    There's never much action in here. Here's a question for all you php guys out there.
    Here's what I want. Let's say I create a simple administration menu for someone to
    Add/Edit/Delete a record/item from a database. Now in the add function, let's say I want to
    store a URL to a image. Rather than having to go in and FTP that image up on the server, how could
    a person create a 'Browse' button and find that image on your harddrive, then create a
    script to upload the image to the server? Just wondering how to do it, or if it could be done e...
  11. Saving A Php Generated Image To The Server - Help Needed Please (5)
    I'm trying to create a script that will generate an image and send it back to the browser while
    saving the same image to the server. I'm doing this because the image should be dynamically
    generated but because of the load that would place on a server if the image happened to be requested
    frequently, I've decided to build in a means to serve the static version of the image most of
    the time. What isn't included here is the function that will check the age of the file in cache
    directory and serve the static image if it is less than an hour old. This isn...
  12. Php: Write Random Text As Image - Having problems, help needed! (3)
    I'm trying to create a script that writes text to an image. CODE header("Content-type:
    image/png"); $_phrases = array( "Test 1", "Test 2", "Test 3", "Test 4", "etc." ); $_rand_phrase
    = $_phrases ; $_image = imagecreatefrompng("gmail.png"); $_user_width =
    imaagettfbbox(9,0,"tahoma.ttf",$_rand_phrase); $_x_value = (200-($user_width + 113)); $_color =
    imagecolorallocate($_image, 165, 164, 164); imagettftext($_image, 9, 0, $_x_value, 16, $_color,
    "tahoma.ttf", $_rand_phrase); imagepng($_image); imagedestroy($_image); I can't see what
    I'm doing wron...
  13. Is It Possible To Create A String Image In Chinese - (0)
    CODE Using imageloadfont $im = imagecreate(50, 20); $black = imagecolorallocate($im, 0, 0, 0);
    $white = imagecolorallocate($im, 255, 255, 255); imagefilledrectangle($im, 0, 0, 49, 19, $white);
    $font = imageloadfont("04b.gdf"); imagestring($im, $font, 0, 0, "Hello", $black); imagepng($im); ?>
      This is an example for illustrating how to create a image including a string with a specific
    font. In the codes, a font named "04b.gdf" is loaded and the string is created according to the
    font. What is the font named gdf? I mean what's the limitation or specificatio...
  14. Forum Signature-image With Php - Use PHP to create an image in real-time (8)
    With interest I have read these Topics. What I want to do with PHP is the following:
    Signatue-image This is the Forum Signature of my cousin, who lives in Europe. The image is
    generated in real-time. (Refresh the page, and see for yourself) My cousin refuses to tell me how
    he did this. But I want to do the same thing, also with PHP. Any ideas? Greetings, John. This
    is NOT a tutorial and in future you should be more careful about where you're posting your
    threads. Moved to Programming > Scripting. ...
  15. Random Image / Random Header - S.O.S. (5)
    im building a website, and i need some help i got 3 frames, navigation, top and main, but i want
    the top banner to have a new tagline with every refresh, just like the header, it dont really matter
    what coding it is, as long as it's not to weird /tongue.gif' border='0'
    style='vertical-align:middle' alt='tongue.gif' /> ...



Looking for resize, image, fly






*SIMILAR VIDEOS*
Searching Video's for resize, image, fly
advertisement




How Do I Resize An Image On The Fly?



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE