Link to home
Start Free TrialLog in
Avatar of paulp75
paulp75Flag for Australia

asked on

unable to pass variable to script

I am using a couple of class files which i picked up which help with processing images, and i'm trying to incorporate them into another script.
problem i'm having though is that i am unable to pass the variables in the url to the script.

i pass this type of variable to the script
test.php?img=testimages/image242.jpg&id=5

put i get the error
The file '$img' cannot be found.

its probably something simple, but i've been up half the night trying to work it out, with no success, so the brain probably isnt functioning quite like it should
any help would be great
thanks
this is the code

<?php
 $id = $_GET["id"];
  $img = $_GET["img"];
    require_once('class.cropinterface.php');
    $ci =& new cropInterface();

    if ($_GET['file'])
    {
        $ci->loadImage($_GET['file']);
        $ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
        header('Content-type: image/jpeg');
        $ci->saveImage('output.jpg', 100);

              $newwidth = '180';
$newheight = '135';
$newfile = '$id.jpg'; // passing the $id = $_GET["id"] variable here
              $filename = 'output.jpg';
              list($width, $height) = getimagesize($filename);
              $source = imagecreatefromjpeg($filename);
              $thumb = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,$newfile, 80);
echo "<img src='newpic.jpg'>";
        exit;

    }

?>

<html>
<body>

<?php

    $ci->setCropDefaultSize(180, 135);
    $ci->setResizing(true);
    $ci->loadInterface('$img'); // $img = $_GET["img"]
    $ci->loadJavaScript();

?>

</body>
</html>
Avatar of alextr2003fr
alextr2003fr

you cannot pass a file through $_GET instead think about a upload system and posting your file useing $_POST
for more information on file uploading : http://fr2.php.net/features.file-upload
hope this helps you.
Avatar of paulp75

ASKER

its just passing the file location, not the file itself.
then try to specify full path
or try something like test.php?file=./testimages/image242.jpg&id=5 instead of test.php?img=testimages/image242.jpg&id=5
ASKER CERTIFIED SOLUTION
Avatar of quad341
quad341

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paulp75

ASKER

thats got  the img sorted out. thanks for that. thats half of the problem solved
now i just need to pass the $id aswell so it will save as 5.jpg etc
instead of just saving as "$id.jpg"

any way to pass that variable also.
i tried
$newfile = ($id.jpg);
but that didnt work though.
any ideas on that one. nearly got it now.
:)
try to store it in a session:

$session_start();
$_SESSION['newfile'] = $id.jpg;
Avatar of paulp75

ASKER

still got problems there.
this is my updated code.


<?php
 $id = $_GET["id"];
  $img = $_GET["img"];
  session_start();
$_SESSION['newfile'] = $id.jpg;
echo "$id";
global $id;
    require_once('class.cropinterface.php');
    $ci =& new cropInterface();

    if ($_GET['file'])
    {
        $ci->loadImage($_GET['file']);
        $ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
        header('Content-type: image/jpeg');
        $ci->saveImage('output.jpg', 100);
$newwidth = '180';
$newheight = '135';
echo "$id";
              $filename = 'output.jpg';
              list($width, $height) = getimagesize($filename);
              $source = imagecreatefromjpeg($filename);
              $thumb = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,$newfile, 80);
echo "<img src='$newfile'>";
exit;
    }
?>
<html>
<body>
<?php
    $ci->setCropDefaultSize(180, 135);
    $ci->setResizing(true);
    $ci->loadInterface($img);
    $ci->loadJavaScript();
?>
</body>
</html>
can u post whats the error message that u got?
Here:

$_SESSION['newfile'] = $id.jpg;

PHP would read that as you are trying to concatenate $id with a constant named jpg, you would need:

"$id.jpg"

or

$id.".jpg"

Additionally, unless you need to pass this file name to multiple pages i do not see a need for sessions here, you could instead just use:

 $id = $_GET['id'].'.jpg';

As a side note, your conditional is a little flawed:

if ($_GET['file'])
    {

would produce an error notice, intead use:

if (isset($_GET['file'])) {

or

if (array_key_exists('file',$_GET)) {

Diablo84
Avatar of paulp75

ASKER

ok i tried all of the methods you suggested too diablo and it wont pass it as 5.jpg still
it saved the image as $id
its just not allowing me to to pass the variable to within the
if (isset($_GET['file'])) {
very strange.
try to correct check something like it :
<?php
  ob_start();
  $id = $_GET["id"];
  $img = $_GET["img"];
  session_start();
  $_SESSION['newfile'] = $id.'.jpg';
  global $id;
    require_once('class.cropinterface.php');
    $ci =& new cropInterface();
    if (isset($_GET['file']))
    {
        $ci->loadImage($_GET['file']);
        $ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
        $ci->saveImage('output.jpg', 100);
        $newwidth = '180';
        $newheight = '135';
        echo "$id";
        $filename = 'output.jpg';
        list($width, $height) = getimagesize($filename);
        $source = imagecreatefromjpeg($filename);
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($thumb,$newfile, 80);
        echo "<img src=\"$newfile\">";
        exit;
    }
  ob_flush();
?>
<html>
<body>
<?php
    $ci->setCropDefaultSize(180, 135);
    $ci->setResizing(true);
    $ci->loadInterface($img);
    $ci->loadJavaScript();
?>
</body>
</html>

also in your class.cropcanvas.php find this function and add global $position, because it is
a undefined variable if it is not global.

    function cropToDimensions($sx, $sy, $ex, $ey)
    {
        $nx = abs($ex - $sx);
        $ny = abs($ey - $sy);
        global $position;
        return ($this->_cropSize($sx, $sy, $nx, $ny, $position, 'cropToDimensions'));
    }

then try to call your script with something like this :
http://localhost/cropimage.php?file=./logo.jpg&sx=0&sy=0&ex=10&ey=14&id=5&img=img.jpg&position=5
Avatar of paulp75

ASKER

still wont pass the variable.
using position or id

i tried adding global $id:
in every function used also.
still nothing
can you try to print the variables out and see what it puts in there?
ex :
echo '<pre>';
print_r($_GET);
echo '</pre>';
Avatar of paulp75

ASKER

i did that within the
 if (isset($_GET['file']))
    {

and got this result


Array
(
    [file] => testimages/image242.jpg
    [sx] => 51
    [sy] => 41
    [ex] => 231
    [ey] => 176
)

are you sure you pass id in your query? ex : index.php?&somestuff&id=something
Avatar of paulp75

ASKER

test1.php?img=testimages/image242.jpg&id=5
is the query
Avatar of paulp75

ASKER

if i do it before the
 if (isset($_GET['file']))
    {  

i get


Array
(
    [img] => testimages/image242.jpg
    [id] => 5
)
do you modify your $_GET somewhere between?
Avatar of paulp75

ASKER

nowhere at all.
it just like it gets lost along the way though somehow
try this as a query :
 test1.php?img=testimages/image242.jpg&id=5&file=testimages/image242.jpg&sx=0&sy=0&ex=10&ey=14&
try to move your require_once('class.cropinterface.php');
on the top because maybe it modifies the $_GET results
try like something like this
<?
  require_once('class.cropinterface.php');
  ...
  $id = $_GET['id'];
  $img = $_GET['img'];  
  ...
Avatar of paulp75

ASKER

nah it still doesnt pass the id variable.
can you show your new code?
with the query you use to call the page too
Avatar of paulp75

ASKER

<?php
require_once('class.cropinterface.php');
$id = $_GET['id'].'.jpg';
$img = $_GET["img"];
$ci =& new cropInterface();
echo '<pre>';
print_r($_GET);
echo '</pre>';
if (isset($_GET['file']))
{
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
header('Content-type: image/jpeg');
$ci->saveImage('output.jpg', 100);
$newwidth = '180';
$newheight = '135';
echo '<pre>';
print_r($_GET);
echo '</pre>';
echo "$id";
$filename = 'output.jpg';
list($width, $height) = getimagesize($filename);
$source = imagecreatefromjpeg($filename);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'$id', 80);
echo "<img src='$id'>";
exit;
 }
?>
<html>
<body>
<?php
$ci->setCropDefaultSize(180, 135);
$ci->setResizing(true);
$ci->loadInterface($img);
$ci->loadJavaScript();
?>
</body>
</html>


calling it with test.php?id=1&img=testimages/image242.jpg

i think that the $id is being lost with the loadJavaScript();
part of it
I test it on my page and it says :
Array
(
    [id] => 1
    [img] => testimages/image242.jpg
)

The file 'testimages/image242.jpg' cannot be found, so the id is passed well but those values are undefined from your query
$_GET['file']
$_GET['sx']
$_GET['sy']
$_GET['ex']
$_GET['ey']
you will need to transform your query to :
test.php?id=1&img=testimages/image242.jpg&file=somefile&sx=value&sy=value&ex=value&ey=value
----------------------------------------------------^ added here
where =value is any integer of your choice if you do not do that then those fields will stay empty
Avatar of paulp75

ASKER

but that wouldnt allow me to use the crop function at all.
the script which i'm trying to incorporate this with is this one
http://php.amnuts.com/index.php?do=view&id=12
so i would get the image into the script which works well now.
then i'd output the file after running it through the crop interface
resize the picture to 180x135
then rename it based on the record in the url
ie id=5
the output file would then be 5.jpg


just seems to not allow the variable to pass through the loadjavascript() part of class.cropinterface.php
you seem to have a logic error if i'm reading this correctly.  your script to resize is making this image.  it cannot make the image and the html page at the same time without physically creating the image file.

the project that you are trying to use sugggests that you pass the dimentions via $_GET variables, but you could just as easily make these static and define them in the script.  then this script would retrieve file and id from $_GET, resize, and output the jpg via setting headers and outputting.  it would be another file that actually would have <img src="YOUR_SCRIPT.php?id=ID_NUM&file=FILE_NAME"> or whatever html you need.
Avatar of paulp75

ASKER

didnt get the entire solution.
but half of it was good enough.
i think i'll have to try to find a different solution for this though