Jump to content

[SOLVED] Strnge function behavior


Suchy

Recommended Posts

So I have a simple function (show.php) that resizes & displays images onto a page.

<?php
session_start();
$link = $_SESSION['link'];
$x_max = $_SESSION['x_max'];

$photo = open (  $link );

function open ($temp) {
        $im = @imagecreatefromjpeg($temp);
        if ($im !== false) { return $im; }

        $im = @imagecreatefromgif($temp);
        if ($im !== false) { return $im; }

        $im = @imagecreatefrompng($temp);
        if ($im !== false) { return $im; }

        return false;
}

$x = imagesx($photo);
$y = imagesy($photo);
$y_max = $y * ($x_max/$x);

$resize = imagecreatetruecolor($x_max, $y_max);
imagecopyresampled($resize, $photo, 0, 0, 0, 0, $x_max, $y_max, $x, $y);

imagejpeg($resize);
?>

 

And it is called inside a loop:

<?php foreach($entriesResults as $temp_img) { 
session_start();
$url = $temp_img['url'];
$_SESSION['link'] = $url;

$max = 75;
$_SESSION['x_max'] = $max;
?>
      <img src="show.php"> </img>	  	  
<? } ?>

 

This works when I limit the query to just 1 item. But when there is no limit, insted of 20 unique pictures. The last image is printed 20 times.

Why is this happening ?

Link to comment
https://forums.phpfreaks.com/topic/53875-solved-strnge-function-behavior/
Share on other sites

I ran into something similar before.  Try making it:

 

<img src="show.php?<?=rand()?>" />

 

Basically your web page is caching (I think you probably are using IE) and the random number "tells" the browser that it is a unique image.

 

Also the "proper" way to close an img tag is with a  /> as there is no closing tag.

You have to have a way to call the file from the show.php try this man.

 

<?php 
foreach($entriesResults as $temp_img) { 
$url = $temp_img['url'];
$max = 75;
echo '<img src="show.php?link=' . $url . '&max=' . $max . '"> </img>';	  	  
} 
?>

 

<?php
$link = $_GET['link'];
$x_max = $_GET['x_max'];

$photo = open (  $link );

function open ($temp) {
        $im = @imagecreatefromjpeg($temp);
        if ($im !== false) { return $im; }

        $im = @imagecreatefromgif($temp);
        if ($im !== false) { return $im; }

        $im = @imagecreatefrompng($temp);
        if ($im !== false) { return $im; }

        return false;
}

$x = imagesx($photo);
$y = imagesy($photo);
$y_max = $y * ($x_max/$x);

$resize = imagecreatetruecolor($x_max, $y_max);
imagecopyresampled($resize, $photo, 0, 0, 0, 0, $x_max, $y_max, $x, $y);

imagejpeg($resize);
?>

 

Try that your code was flawed because you were not storing it in an array, and the show.php does not get called until the entire page is displayed which as it happens would be calling your last session entry. Remember php is PRE HYPER-TEXT PROCESSING, means it processes everything first before it gets sent to the browser.

 

Anyhow that should work for you.

<?php 
foreach($entriesResults as $temp_img) { 
$url = $temp_img['url'];
$max = 75;
echo '<img src="show.php?link=' . $url . '&x_max=' . $max . '"> </img>';	  	  
} 
?>

 

Did not see that $max was turned into $x_max on the next page.

 

If that does not work, post some of the < img code the script is producing, it can be fun sometimes to code blind, but very inefficient.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.