Jump to content

Recommended Posts

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 image
if ( $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 image
if ( $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 thumb
imagecopyresized($thumb, $pic, 0, 0, 0, 0,
$sirina, $theight, $width, $height);

// Change page type to a jpeg
header ("Content-type: image/jpeg");

// Create jpeg image from thumbnail
imagejpeg($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 ?
Link to comment
https://forums.phpfreaks.com/topic/27201-header-problem-or-what/
Share on other sites

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
You can remove the header from that function - it will still render OK.

OR

use ob_start and ob_endflush

OR

Generate the entre page output in a string and echo it out when all other code has finished

OR similar to above but using something like smarty templates.
<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]

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">

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.