• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

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>
0
paulp75
Asked:
paulp75
  • 14
  • 13
  • 2
  • +2
1 Solution
 
alextr2003frCommented:
you cannot pass a file through $_GET instead think about a upload system and posting your file useing $_POST
0
 
alextr2003frCommented:
for more information on file uploading : http://fr2.php.net/features.file-upload
hope this helps you.
0
 
paulp75Author Commented:
its just passing the file location, not the file itself.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
alextr2003frCommented:
then try to specify full path
0
 
alextr2003frCommented:
or try something like test.php?file=./testimages/image242.jpg&id=5 instead of test.php?img=testimages/image242.jpg&id=5
0
 
quad341Commented:
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
 
paulp75Author Commented:
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
 
Marvin_CarredoCommented:
try to store it in a session:

$session_start();
$_SESSION['newfile'] = $id.jpg;
0
 
paulp75Author Commented:
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
 
Marvin_CarredoCommented:
can u post whats the error message that u got?
0
 
Diablo84Commented:
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
 
paulp75Author Commented:
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
 
alextr2003frCommented:
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
 
paulp75Author Commented:
still wont pass the variable.
using position or id

i tried adding global $id:
in every function used also.
still nothing
0
 
alextr2003frCommented:
can you try to print the variables out and see what it puts in there?
ex :
echo '<pre>';
print_r($_GET);
echo '</pre>';
0
 
paulp75Author Commented:
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
 
alextr2003frCommented:
are you sure you pass id in your query? ex : index.php?&somestuff&id=something
0
 
paulp75Author Commented:
test1.php?img=testimages/image242.jpg&id=5
is the query
0
 
paulp75Author Commented:
if i do it before the
 if (isset($_GET['file']))
    {  

i get


Array
(
    [img] => testimages/image242.jpg
    [id] => 5
)
0
 
alextr2003frCommented:
do you modify your $_GET somewhere between?
0
 
paulp75Author Commented:
nowhere at all.
it just like it gets lost along the way though somehow
0
 
alextr2003frCommented:
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
 
alextr2003frCommented:
try to move your require_once('class.cropinterface.php');
on the top because maybe it modifies the $_GET results
0
 
alextr2003frCommented:
try like something like this
<?
  require_once('class.cropinterface.php');
  ...
  $id = $_GET['id'];
  $img = $_GET['img'];  
  ...
0
 
paulp75Author Commented:
nah it still doesnt pass the id variable.
0
 
alextr2003frCommented:
can you show your new code?
0
 
alextr2003frCommented:
with the query you use to call the page too
0
 
paulp75Author Commented:
<?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
 
alextr2003frCommented:
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
 
paulp75Author Commented:
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
 
quad341Commented:
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
 
paulp75Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 14
  • 13
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now