FFFF Posted November 14, 2006 Share Posted November 14, 2006 Hello!I have the next problem: On one page I have to show a picture that I take from a web link . That picture have to be resized to a thumbnail but NOT to be stored in to a database, but has to be resized on the fly. Here is the function that dooes this :[code]function smanji_sliku($url){$img = getimagesize($url) or die();// Checks if URL is imageif ( $img[2] == 1 ){$pic = imagecreatefromgif( $url ) or die();}else if ( $img[2] == 2 ){$pic = imagecreatefromjpeg( $url ) or die();}else if ( $img[2] ==3 ){$pic = imagecreatefrompng( $url ) or die();}else{exit();}// If an image is found and we can determine that it is an imageif ( $pic ){// Get width and height from the image$width = imagesx( $pic );$height = imagesy( $pic );// Width that thumbnail should be$sirina = 150;// Calculate the new height$theight = $sirina * $height / $width;// Create new image$thumb = @imagecreatetruecolor ( $sirina, $theight )or die ("Can't create Image!");// Resize the image into a thumbimagecopyresized($thumb, $pic, 0, 0, 0, 0,$sirina, $theight, $width, $height);// Change page type to a jpegheader ("Content-type: image/jpeg");// Create jpeg image from thumbnailimagejpeg($thumb,"",75);}else{exit();}}[/code]the function works fine but I need to show some text after that picture.. when I try to output text after the image the next error occurs (Warning: Cannot modify header information - headers already sent by (output started at C:\inet\www\ispis2.php:9) in C:\inet\www\ispis2.php on line 102) .. I think this is some kind of problem with the header (header ("Content-type: image/jpeg");) .... I don't know how to show both an image and some text on the same page .. ? Can that be done with this kind of "on the fly" image resizeing function? or I have to store the picture in a database ? Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/ Share on other sites More sharing options...
c_shelswell Posted November 14, 2006 Share Posted November 14, 2006 Ok i'm really new to this so i'm sure someone will provide a better answer (or even a correct one). I think that you can't set the headers if any text or html has been sent before you modify the header. I think header changes must come first.Hope that makes some sense.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124377 Share on other sites More sharing options...
ToonMariner Posted November 14, 2006 Share Posted November 14, 2006 You can remove the header from that function - it will still render OK.ORuse ob_start and ob_endflushORGenerate the entre page output in a string and echo it out when all other code has finishedOR similar to above but using something like smarty templates. Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124378 Share on other sites More sharing options...
FFFF Posted November 14, 2006 Author Share Posted November 14, 2006 thanx for the answers @ToonMariner when I removed the headers it doesn't work at all .. the picture doesnt show .Can you bee more specific about that use ob_start and ob_endflush .. how to use that ? or any other help would be great thnx Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124454 Share on other sites More sharing options...
haaglin Posted November 14, 2006 Share Posted November 14, 2006 Are you using that code in the same page as other things like text and stuff? When you are using the image functions, you need that in its own file, like thumb.php and point to that in your image tag. Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124456 Share on other sites More sharing options...
FFFF Posted November 14, 2006 Author Share Posted November 14, 2006 you think something like POST method or something ? pls can you write me how to point from img tag ? .. im a beginner Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124482 Share on other sites More sharing options...
haaglin Posted November 14, 2006 Share Posted November 14, 2006 <img src="image.php?src=image.png&wmax=100&hmax=100" alt="" border="0">An example of image.php, this makes a png image with the specs provided above(max 100px w and h) :[code]$src=$_GET['src'];$i = strrpos($src,".");if (!$i) { die(); }$l = strlen($src) - $i;$filetype = substr($src,$i+1,$l);if ( $filetype == "png") { $source = imagecreatefrompng($src);}else if ( $filetype == "jpg") { $source = imagecreatefromjpeg($src);} else if ( $filetype == "jpeg") { $source = imagecreatefromjpeg($src);} else if ( $filetype == "gif") { $source = imagecreatefromgif($src);} else { die();}header("Content-type: image/png");$orig_w=imagesx($source);$orig_h=imagesy($source);$wmax=$_GET[wmax];$hmax=$_GET[hmax];if(!empty($wmax) || !empty($hmax)) { if ($orig_w>$wmax || $orig_h>$hmax) { $thumb_w=$wmax; $thumb_h=$hmax; if ($thumb_w/$orig_w*$orig_h>$thumb_h) $thumb_w=round($thumb_h*$orig_w/$orig_h); else $thumb_h=round($thumb_w*$orig_h/$orig_w); } else { $thumb_w=$orig_w; $thumb_h=$orig_h; }}$thumb=imagecreatetruecolor($thumb_w,$thumb_h);imagecopyresampled($thumb,$source,0,0,0,0,$thumb_w,$thumb_h,$orig_w,$orig_h); imagepng($thumb);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124499 Share on other sites More sharing options...
FFFF Posted November 14, 2006 Author Share Posted November 14, 2006 Great man ! but one more question :) I get the image url that needs to be resized from an array so what should i write in src= ? ... I mean how to implement that php array in that html tag<img src="image.php?[color=red]src=image.png[/color]&wmax=100&hmax=100" alt="" border="0"> Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124511 Share on other sites More sharing options...
haaglin Posted November 14, 2006 Share Posted November 14, 2006 Not sure how your array is, but something like this:[code]echo '<img src="image.php?src=' . $array[src] . '&wmax=100&hmax=100" alt="" border="0">';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124514 Share on other sites More sharing options...
haaglin Posted November 14, 2006 Share Posted November 14, 2006 Or as function:[code]function smanji_sliku($url){ return '<img src="image.php?src=' . $url . '&wmax=100&hmax=100" alt="" border="0">';}[/code]then you can write [code]echo smanji_sliku($url);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124516 Share on other sites More sharing options...
FFFF Posted November 14, 2006 Author Share Posted November 14, 2006 thanx man !! I learned a lot ;D Quote Link to comment https://forums.phpfreaks.com/topic/27201-header-problem-or-what/#findComment-124542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.