Page 1 of 1

imageresize() - function producing unknown errors

Posted: Sun Jan 17, 2010 12:53 pm
by simonmlewis
Hi

Code: Select all

<?php
 
//get the image size of the picture and load it into an array
$mysock = getimagesize("images/productphotos/17175032Julie_Walters_1243785c.jpg");
 
?>
 
<!-using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->
 
<img src="images/productphotos/17175032Julie_Walters_1243785c.jpg" <?php imageResize($mysock[0],  
$mysock[1], 150); ?>>
Can anyone please identify why when I run this within test.inc, it produces this error:
Fatal error: Call to undefined function imageResize() in /usr/local/psa/home/vhosts/site.co.uk/httpdocs/beta/includes/test.inc on line 12
..... AFTER... the image is shown on the screen.

I need to find an imageresize function that resizes it when the users uploads the file. This is part of a tutorial I have found, but cannot get beyond it because I don't understand the error or how to resolve.

Re: imageresize() - function producing unknown errors

Posted: Sun Jan 17, 2010 7:30 pm
by McInfo
The error occurs because the imageResize() function is not defined (hence "Call to undefined function imageResize()"). You probably didn't copy enough of the code from the tutorial. But forget that tutorial. It is useless for what you are trying to do.
  1. When the user uploads an image file, you need to open the file as an image resource using the appropriate imagecreatefrom*() function.
  2. Create a second image resource with imagecreate() or imagecreatetruecolor() so you have a place to put the resized image data.
  3. Copy and resize an area from the original image to the second image using imagecopyresized() or imagecopyresampled().
  4. Save the second image to a file using imagejpeg() or other image*() function.
PHP Manual: GD and Image Functions

There are also alternatives to GD like ImageMagick. It is possible to resize an image file using a command-line executable.

Edit: This post was recovered from search engine cache.

Re: imageresize() - function producing unknown errors

Posted: Thu Jan 21, 2010 7:32 am
by simonmlewis
What is the difference between all the imagecreatefrom*() functions? I've never written a function from scratch.

So going from never writing one, to trying to work it all out, and do something this detailed is quite a stretch for me!!

Re: imageresize() - function producing unknown errors

Posted: Thu Jan 21, 2010 8:38 am
by simonmlewis
Ok - I've got the script below working, and appears to be using much of what has been suggested to me.
I also got it to rename the files, so there are no overridden files from customers.

Trouble is, it's not now inserting the data in the DB.
I have echoed out the Variables AFTER the mysql_query code, to see if it gets past it - and that all works.
I have echoed out the $newid, however, and that just produces "0".

Before I placed the file/image resizer into the script, the INSERT worked. Have I done something daft here??

Code: Select all

<?php
$userid=$_POST['userid'];
$catid=$_POST['catid'];
$catname=$_POST['catname'];
$subid=$_POST['subid'];
$subname=$_POST['subname'];
$title=$_POST['title'];
$subtitle=$_POST['subtitle'];
$description=$_POST['description'];
$condition=$_POST['condition'];
$video=$_POST['video'];
$method=$_POST['method'];
$today = (date('Y-m-d'));
$price = sprintf('%0.2f', preg_replace('/[^0-9.]/', '', $_POST['price']));
$pp=$_POST['pp'];
$pic=($_FILES['photo']['name']);
include "dbconn.php";
 
if ($pic == NULL) 
{ 
   if(get_magic_quotes_gpc()) {
      $input = array(&$_GET, &$_POST, &$_COOKIE, &$_ENV, &$_SERVER);
     
      while(list($k, $v) = each($input)) {
        foreach($v as $key => $val) {
          if(!is_array($val)) {
            $input[$k][$key] = stripslashes($val);
            continue;
          }
          $input[] =& $input[$k][$key];
        }
      }
      unset($input);
    }
 
$posted_description = (get_magic_quotes_gpc()) ? stripslashes($_POST['description']) : $_POST['description'];
$description=mysql_real_escape_string($posted_description);
 
$posted_title = (get_magic_quotes_gpc()) ? stripslashes($_POST['title']) : $_POST['title'];
$title=mysql_real_escape_string($posted_title);
 
$posted_subtitle = (get_magic_quotes_gpc()) ? stripslashes($_POST['subtitle']) : $_POST['subtitle'];
$subtitle=mysql_real_escape_string($posted_subtitle);
 
mysql_query("INSERT INTO products
(userid, catid, catname, subid, subname, title, subtitle, description, condition, video, method, price, pp, creationdate) VALUES 
('$userid', '$catid', '$catname', '$subid', '$subname', '$title', '$subtitle', '$description', '$condition', '$video', '$method', '$price', '$pp', '$today')");
 
$newid = mysql_insert_id();
 
$result = mysql_query ("SELECT * FROM products WHERE id = '$newid'");
while ($row = mysql_fetch_object($result))
      {
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=product&id=$row->id&menu=sub&c=$row->catid&cname=$row->catname&sname=$row->subname&head=$row->title&share=yes'>";
      } mysql_free_result($result);
}
 
elseif ($pic != NULL) 
{
error_reporting(0);
 
$change="";
$abc="";
 
 define ("MAX_SIZE","400");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 
 $errors=0;
  
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
    $image =$_FILES["photo"]["name"];
    $uploadedfile = $_FILES['photo']['tmp_name'];    
 
    if ($image) 
    {
    
        $filename = stripslashes($_FILES['photo']['name']);
    
        $extension = getExtension($_FILES['photo']['name']);
        $extension = strtolower($extension);
        
        
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {
        
            $change='<div class="msgdiv">Unknown Image extension </div> ';
            $errors=1;
        }
        else
        {
 
 $size=filesize($_FILES['photo']['tmp_name']);
 
 
if ($size > MAX_SIZE*1024)
{
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
}
 
 
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['photo']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
 
}
else if($extension=="png")
{
$uploadedfile = $_FILES['photo']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
 
}
else 
{
$src = imagecreatefromgif($uploadedfile);
}
 
echo $scr;
 
list($width,$height)=getimagesize($uploadedfile);
 
 
$newwidth=400;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
 
 
$newwidth1=130;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
 
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
 
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
 
$pic=($_FILES['photo']['name']);
srand(time());
$random = (rand()%99999999);
$newname="$random"."$pic";
 
$filename = "images/productphotos/". $newname;
$filename1 = "images/productphotos/small/". $newname;
 
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
 
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
 
}
 
$posted_description = (get_magic_quotes_gpc()) ? stripslashes($_POST['description']) : $_POST['description'];
$description=mysql_real_escape_string($posted_description);
 
$posted_title = (get_magic_quotes_gpc()) ? stripslashes($_POST['title']) : $_POST['title'];
$title=mysql_real_escape_string($posted_title);
 
$posted_subtitle = (get_magic_quotes_gpc()) ? stripslashes($_POST['subtitle']) : $_POST['subtitle'];
$subtitle=mysql_real_escape_string($posted_subtitle);
 
mysql_query("INSERT INTO products
(userid, catid, catname, subid, subname, title, subtitle, description, condition, video, method, price, pp, photoprimary, creationdate) VALUES 
('$userid', '$catid', '$catname', '$subid', '$subname', '$title', '$subtitle', '$description', '$condition', '$video', '$method', '$price', '$pp', '$newname', '$today')");
 
$newid = mysql_insert_id();
echo "$newid";
$result = mysql_query ("SELECT * FROM products WHERE id = '$newid'");
while ($row = mysql_fetch_object($result))
      {
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=product&id=$row->id&menu=sub&c=$row->catid&cname=$row->catname&sname=$row->subname&head=$row->title&share=yes'>";
}
      mysql_free_result($result);
      }
    mysql_close($sqlconn);
?>

Re: imageresize() - function producing unknown errors

Posted: Thu Jan 21, 2010 9:22 am
by simonmlewis
Even while trying to process it with no picture file, it still produces a ZERO as $newid.

I'm stumped!!

Code: Select all

<?php
$userid=$_COOKIE['id'];
$catid=$_POST['catid'];
$catname=$_POST['catname'];
$subid=$_POST['subid'];
$subname=$_POST['subname'];
$title=$_POST['title'];
$subtitle=$_POST['subtitle'];
$description=$_POST['description'];
$condition=$_POST['condition'];
$video=$_POST['video'];
$method=$_POST['method'];
$swapfor=$_POST['swapfor'];
$today = (date('Y-m-d'));
$price = sprintf('%0.2f', preg_replace('/[^0-9.]/', '', $_POST['price']));
$pp=$_POST['pp'];
$photo=$_POST['photo'];
$pic=($_FILES['photo']['name']);
include "dbconn.php";
 
if ($pic == NULL) 
{ 
   if(get_magic_quotes_gpc()) 
   {
      $input = array(&$_GET, &$_POST, &$_COOKIE, &$_ENV, &$_SERVER);
     
      while(list($k, $v) = each($input)) 
      {
        foreach($v as $key => $val) 
        {
          if(!is_array($val)) 
          {
            $input[$k][$key] = stripslashes($val);
            continue;
          }
          $input[] =& $input[$k][$key];
        }
      }
      unset($input);
    }
 
$posted_description = (get_magic_quotes_gpc()) ? stripslashes($_POST['description']) : $_POST['description'];
$description=mysql_real_escape_string($posted_description);
 
$posted_title = (get_magic_quotes_gpc()) ? stripslashes($_POST['title']) : $_POST['title'];
$title=mysql_real_escape_string($posted_title);
 
$posted_subtitle = (get_magic_quotes_gpc()) ? stripslashes($_POST['subtitle']) : $_POST['subtitle'];
$subtitle=mysql_real_escape_string($posted_subtitle);
 
$posted_swapfor = (get_magic_quotes_gpc()) ? stripslashes($_POST['swapfor']) : $_POST['swapfor'];
$swapfor=mysql_real_escape_string($posted_swapfor);
 
mysql_query("INSERT INTO products
(userid, 
catid, 
catname, 
subid, 
subname, 
title, 
subtitle, 
description, 
condition, 
video, 
method, 
price, 
pp, 
swapfor,
creationdate) VALUES 
('$userid', 
'$catid', 
'$catname', 
'$subid', 
'$subname', 
'$title', 
'$subtitle', 
'$description', 
'$condition', 
'$video', 
'$method', 
'$price', 
'$pp', 
'$swapfor',
'$today')");
 
$newid = mysql_insert_id();
echo "$newid";
$result = mysql_query ("SELECT * FROM products WHERE id = '$newid'");
while ($row = mysql_fetch_object($result))
      {
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=product&id=$row->id&menu=sub&c=$row->catid&cname=$row->catname&sname=$row->subname&head=$row->title&share=yes'>";
      } mysql_free_result($result);
}
    mysql_close($sqlconn);
?>

Re: imageresize() - function producing unknown errors

Posted: Thu Jan 21, 2010 9:45 am
by simonmlewis
This is the same part of the code, trying to render what is being passed through from the form.

As you can see, everything that is needed is being passed through.

Code: Select all

<?php
$userid=$_COOKIE['userid'];
$catid=$_POST['catid'];
$catname=$_POST['catname'];
$subid=$_POST['subid'];
$subname=$_POST['subname'];
$title=$_POST['title'];
$subtitle=$_POST['subtitle'];
$description=$_POST['description'];
$condition=$_POST['condition'];
$video=$_POST['video'];
$method=$_POST['method'];
$swapfor=$_POST['swapfor'];
$today = (date('Y-m-d'));
$price = sprintf('%0.2f', preg_replace('/[^0-9.]/', '', $_POST['price']));
$pp=$_POST['pp'];
$photo=$_POST['photo'];
$pic=($_FILES['photo']['name']);
include "dbconn.php";
 
if ($pic == NULL) 
{ 
   if(get_magic_quotes_gpc()) 
   {
      $input = array(&$_GET, &$_POST, &$_COOKIE, &$_ENV, &$_SERVER);
     
      while(list($k, $v) = each($input)) 
      {
        foreach($v as $key => $val) 
        {
          if(!is_array($val)) 
          {
            $input[$k][$key] = stripslashes($val);
            continue;
          }
          $input[] =& $input[$k][$key];
        }
      }
      unset($input);
    }
 
$posted_description = (get_magic_quotes_gpc()) ? stripslashes($_POST['description']) : $_POST['description'];
$description=mysql_real_escape_string($posted_description);
 
$posted_title = (get_magic_quotes_gpc()) ? stripslashes($_POST['title']) : $_POST['title'];
$title=mysql_real_escape_string($posted_title);
 
$posted_subtitle = (get_magic_quotes_gpc()) ? stripslashes($_POST['subtitle']) : $_POST['subtitle'];
$subtitle=mysql_real_escape_string($posted_subtitle);
 
$posted_swapfor = (get_magic_quotes_gpc()) ? stripslashes($_POST['swapfor']) : $_POST['swapfor'];
$swapfor=mysql_real_escape_string($posted_swapfor);
echo " $userid<br/>
$catid<br/>
$catname<br/>
$subid<br/>
$subname<br/>
$title<br/>
$subtitle<br/>
$description<br/>
$condition<br/>
$method<br/>
$swapfor<br/>
$today<br/>
$price<br/>
$pp<br/>
$pic<br/>";
mysql_query("INSERT INTO products
(userid, catid, catname, subid, subname, title, subtitle, description, condition, video, method, price, pp, swapfor, creationdate) VALUES ('$userid', '$catid', '$catname', '$subid', '$subname', '$title', '$subtitle', '$description', '$condition', '$video', '$method', '$price', '$pp', '$swapfor','$today')")or die(mysql_error());
The mysql_error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition, video, method, price, pp, swapfor, creationdate) VALUES ('2', '4', 'B' at line 2
I haven't a clue where that is, because line 2 is '$userid=$_COOKIE['userid'];'.

Re: imageresize() - function producing unknown errors

Posted: Thu Jan 21, 2010 12:33 pm
by McInfo
The error is a MySQL error, not a PHP error. It refers to line 2 of the query, not line 2 of the script. The error occurs because "condition" is a reserved word in MySQL. Notice that the error says "...near 'condition...". If a reserved word is used as a field name, it needs to have backticks (`) around it when it appears in a query.

I thought you were trying to resize an uploaded image. What does the database have to do with that?

Why is stripslashes() used twice on some inputs?

Edit: This post was recovered from search engine cache.

RESOLVED.Re: imageresize() - function producing unknown erro

Posted: Thu Jan 21, 2010 12:41 pm
by simonmlewis
It was all part of the same problem.
In the resizing image issue, I came across that error.

Someone else has written to me about the same 'condition' solution, so thank you for yours too.

Re: imageresize() - function producing unknown errors

Posted: Thu Jan 21, 2010 12:48 pm
by McInfo
I forgot to answer your other questions before. I tried to edit my last post with these responses, but then I saw that you had already replied.
simonmlewis wrote:Even while trying to process it with no picture file, it still produces a ZERO as $newid.
I see no evidence of an auto-increment field in your table. See mysql_insert_id().
simonmlewis wrote:What is the difference between all the imagecreatefrom*() functions?
They each deal with a different image format. For example, imagecreatefrompng() creates an image resource from a PNG-formatted image file.

Edit: This post was recovered from search engine cache.