[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

unable to pass variable to script

Posted on 2005-04-15
32
Medium Priority
?
296 Views
Last Modified: 2008-01-09
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>
0
Comment
Question by:paulp75
  • 14
  • 13
  • 2
  • +2
32 Comments
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13795986
you cannot pass a file through $_GET instead think about a upload system and posting your file useing $_POST
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13795993
for more information on file uploading : http://fr2.php.net/features.file-upload
hope this helps you.
0
 
LVL 6

Author Comment

by:paulp75
ID: 13796039
its just passing the file location, not the file itself.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13796049
then try to specify full path
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13796130
or try something like test.php?file=./testimages/image242.jpg&id=5 instead of test.php?img=testimages/image242.jpg&id=5
0
 
LVL 6

Accepted Solution

by:
quad341 earned 2000 total points
ID: 13796175
the problem is here:

 $ci->loadInterface('$img'); // $img = $_GET["img"]

this should be
$ci->loadInterface($img); //or $ci->loadInterface("$img");

this is because single quotes are passing that as a static string and not parsing the variable.  you can either remove all quotes and just use the variable or double quotes which parse variables
0
 
LVL 6

Author Comment

by:paulp75
ID: 13796211
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.
:)
0
 
LVL 2

Expert Comment

by:Marvin_Carredo
ID: 13796300
try to store it in a session:

$session_start();
$_SESSION['newfile'] = $id.jpg;
0
 
LVL 6

Author Comment

by:paulp75
ID: 13796353
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>
0
 
LVL 2

Expert Comment

by:Marvin_Carredo
ID: 13796487
can u post whats the error message that u got?
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 13796869
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
0
 
LVL 6

Author Comment

by:paulp75
ID: 13799651
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.
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799725
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
0
 
LVL 6

Author Comment

by:paulp75
ID: 13799840
still wont pass the variable.
using position or id

i tried adding global $id:
in every function used also.
still nothing
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799844
can you try to print the variables out and see what it puts in there?
ex :
echo '<pre>';
print_r($_GET);
echo '</pre>';
0
 
LVL 6

Author Comment

by:paulp75
ID: 13799862
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
)

0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799865
are you sure you pass id in your query? ex : index.php?&somestuff&id=something
0
 
LVL 6

Author Comment

by:paulp75
ID: 13799871
test1.php?img=testimages/image242.jpg&id=5
is the query
0
 
LVL 6

Author Comment

by:paulp75
ID: 13799875
if i do it before the
 if (isset($_GET['file']))
    {  

i get


Array
(
    [img] => testimages/image242.jpg
    [id] => 5
)
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799879
do you modify your $_GET somewhere between?
0
 
LVL 6

Author Comment

by:paulp75
ID: 13799884
nowhere at all.
it just like it gets lost along the way though somehow
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799889
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&
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799899
try to move your require_once('class.cropinterface.php');
on the top because maybe it modifies the $_GET results
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13799905
try like something like this
<?
  require_once('class.cropinterface.php');
  ...
  $id = $_GET['id'];
  $img = $_GET['img'];  
  ...
0
 
LVL 6

Author Comment

by:paulp75
ID: 13800906
nah it still doesnt pass the id variable.
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13800908
can you show your new code?
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13800916
with the query you use to call the page too
0
 
LVL 6

Author Comment

by:paulp75
ID: 13803069
<?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
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13803089
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
0
 
LVL 6

Author Comment

by:paulp75
ID: 13803171
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
0
 
LVL 6

Expert Comment

by:quad341
ID: 13830263
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.
0
 
LVL 6

Author Comment

by:paulp75
ID: 13977974
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question