Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Not sure why Im getting this error

  • 29-08-2008 04:35PM
    #1
    Registered Users, Registered Users 2 Posts: 788 ✭✭✭


    Wondering if can someone help me. I'm uploading images through a form and trying to crop them. I found a function to crop them online. I have this in 'crop.inc'. I include this as
    require('crop.inc');
    
    and call it as
     cropImage(220, 160, '/images/prod_images/main/$id-1.jpg', 'jpg', '/images/prod_images/main/$id-1.jpg');
    

    this is the crop.inc function that i got online
    <?php
    	  
    
    function cropImage($nw, $nh, $source, $stype, $dest) {
    
    		  $size = getimagesize($source);
    		 $w = $size[0];
    		  $h = $size[1];
    
    		  switch($stype) {
    
    			  case 'gif':
    			  $simg = imagecreatefromgif($source);
    
    			  break;
    
    			  case 'jpg':
    			  $simg = imagecreatefromjpeg($source);
    
    			  break;
    
    			  case 'png':
    			  $simg = imagecreatefrompng($source);
    
    			  break;
    
    		  }
    
    		  $dimg = imagecreatetruecolor($nw, $nh);
    		  $wm = $w/$nw;
    		  $hm = $h/$nh;
    
    		  $h_height = $nh/2;
    		  $w_height = $nw/2;
    
    		  if($w> $h) {
    
    			  $adjusted_width = $w / $hm;
    			  $half_width = $adjusted_width / 2;
    			  $int_width = $half_width - $w_height;
    
    imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
    
    		  } elseif(($w <$h) || ($w == $h)) {
    			  $adjusted_height = $h / $wm;
    			  $half_height = $adjusted_height / 2;
    			  $int_height = $half_height - $h_height;
    			  imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
    
    		  } else {
    
    			  imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
    
    		  }
    
    		  imagejpeg($dimg,$dest,100);
    
    	  }
    
    ?>
    
    I think it may be not working as I am using a variable in the image name. I need to use a variable though. Anyone know any way around this?


Comments

  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Could you try this?

    [php]
    cropImage(220, 160, '/images/prod_images/main/'.$id-1.jpg.'', 'jpg', '/images/prod_images/main/'.$id-1.jpg.'');
    [/php]


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    AARRRGH wrote: »
    Could you try this?

    [php]
    cropImage(220, 160, '/images/prod_images/main/'.$id-1.jpg.'', 'jpg', '/images/prod_images/main/'.$id-1.jpg.'');
    [/php]
    Thats it.

    Remember OP, variables in double quotes "$myvar", are interpreted, IE -the variables value is placed there.
    Anything enclosed in single quotes are string literals, meaning pass or print literally anything enclosed print '$myvar' will print exactly '$myvar' and not the value of var.

    Same goes for escape characters such as new line "\n" or tab "\t".

    The print out of $id-1.jpg in the warning is a hint that $id-1.jpg is not getting replaced with the actual value.


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Thanks a million I'll give it a go on Monday :D


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    If I use the above I get
    Parse error: syntax error, unexpected T_STRING in /hsphere/local/home/test/test/admin/test.php on line 69 
    

    And if I use
    cropImage(220, 160, '/images/prod_images/main/'.$id.'-1.jpg', 'jpg', '/images/prod_images/main/'.$id.'-1.jpg');
    


    I get the error
    Warning: getimagesize(/images/prod_images/main/138-1.jpg): failed to open stream: No such file or directory in /hsphere/local/home/test/test/admin/crop.inc on line 8 Warning: imagecreatefromjpeg(/images/prod_images/main/138-1.jpg): failed to open stream: No such file or directory in /hsphere/local/home/test/test/admin/crop.inc on line 26 Warning: Division by zero in /hsphere/local/home/test/test/admin/crop.inc on line 75 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /hsphere/local/home/test/admin/crop.inc on line 83 Warning: imagejpeg(): Unable to open '/images/prod_images/main/138-1.jpg' for writing in /hsphere/local/home/test/test/admin/crop.inc on line 94 Warning: getimagesize(/images/prod_images/138-1.jpg): failed to open stream: No such file or directory in /hsphere/local/home/test/test/admin/crop.inc on line 8 Warning: imagecreatefromjpeg(/images/prod_images/138-1.jpg): failed to open stream: No such file or directory in /hsphere/local/home/test/test/admin/crop.inc on line 26 Warning: Division by zero in /hsphere/local/home/test/test/admin/crop.inc on line 75 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/crop.inc on line 83 Warning: imagejpeg(): Unable to open '/images/prod_images/138-1.jpg' for writing in /hsphere/local/home/test/test/admin/crop.inc on line 94 Warning: imagejpeg(): Unable to open '/hsphere/local/home/test/test/admin/images/prod_images/main/138-1.jpg' for writing in /hsphere/local/home/test/test/admin/resize.inc on line 25 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/test/test/admin/crop.inc:8) in /hsphere/local/home/test/test/admin/padd.php on line 75 
    
    :(


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    The cropImage function takes an image path from the filesystem, not from the web.

    So when you're calling pointing at "/images/prod_images/main/", it's looking for a folder called "images" in the root of the filesystem as opposed to the root of the web server.

    Use "/hsphere/local/home/test/images/prod_images/main/" instead.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Thanks :) but now Im getting:
    Warning: imagejpeg(): Unable to open '/hsphere/local/home/test/test/images/prod_images/main/138-1.jpg' for writing in /hsphere/local/home/test/test/admin/crop.inc on line 94 Warning: imagejpeg(): Unable to open '/hsphere/local/home/test/test/images/prod_images/main/138-1.jpg' for writing in hsphere/local/home/test/test/admin/resize.inc on line 25 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/test/test/admin/crop.inc:94) in /hsphere/local/home/test/test/admin/padd.php on line 75 
    

    :confused:


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    All fun and games.

    You need to open up the folder /hsphere/local/home/test/test/images/prod_images/main/ as writeable to the webserver user.


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Hmm permissions are already set to 757. Oh well. I'll figure it out, thanks for your help :D


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Are you sure that /hsphere/local/home/test/test/images/prod_images/main/ is the correct directory (2 test folders)


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Yup


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Chmod it to 777 so :)


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Its all set to 777 and still getting the error of unable to write. Getting stressed now as this has to be working by tomorrow afternoon :( Anyone know of a cropping image function that actually works :P


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    By any chance are your pictures capital .JPG's - Remember Linux is case sensitive.
    Some programs on windows (MS Paint) for example, have habit of saving to *.JPG instead of *.jpg

    Edit - just a pure chance - I don't know if the file is locked since you are writing to the file you are resizing. Try imagedestroy($simg)

    [php]
    : : :

    imagedestroy($simg);
    imagejpeg($dimg,$dest,100);

    }

    ?>
    [/php]


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    No they are lowercase. Is this crop function working for anyone else? I really cant understand why it isnt writing the image to the folder :( I have no clue of these functions I just got one online and I need to have it working by tomorrow afternoon


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    I think you are going to have the same problem with another crop script as its file permissions thats the problem here.

    I would try and create a script that creates a simple text file in that directory and see if you can.

    [php]
    <?php
    $outputfile = "/hsphere/local/home/test/test/images/prod_images/main/testfile.txt";
    $fh = fopen($outputfile, 'w') or die("Can't open for write, definetely permissions prob!");
    $tempStr = "Testing";
    fwrite($outputfile, $tempStr);
    fwrite($fh, $stringData);
    fclose($fh);

    ?>
    [/php]

    run this script and see if the text file exists in the directory. At least this will count out a few problems for you.


    By the way - have you jsut Chmodded the images directory, make sure to chmod the main/ directory to 777


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    That's a good idea thanks i'll test that in the morning :) Also does anyone know the correct syntax for cropping an image in php using mogrify?


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Warning: fwrite(): supplied argument is not a valid stream resource in /hsphere/local/home/test/test/test.php on line 5 
    


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Sorry that was wrong by me.

    [php]
    <?php
    $outputfile = "/hsphere/local/home/test/test/images/prod_images/main/testfile.txt";
    $fh = fopen($outputfile, 'w') or die("Can't open for write, definetely permissions prob!");
    $tempStr = "Testing";
    fwrite($fh, $tempStr);
    fclose($fh);

    ?> [/php]

    Now see if that file testfile.txt exists in the directory and it has the text Testing in it.


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Ok ive now discovered that the cropping and resizing functions are working (woo) but i still get this lovely error when i upload (but when i check the images in the folder they are there and appear cropped)
    Warning: imagecreatefromjpeg(/142-1.jpg): failed to open stream: No such file or directory in /hsphere/local/home/test/test/admin/resize.inc on line 34 Warning: imagesx(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test.ie/admin/resize.inc on line 36 Warning: imagesy(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 37 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 46 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 47 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 50 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 50 Warning: imagecreatetruecolor(): Invalid image dimensions in /hsphere/local/home/test/test/admin/resize.inc on line 54 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 55 Warning: imagejpeg(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 56 Warning: imagedestroy(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 57 Warning: imagecreatefromjpeg(/142-1.jpg): failed to open stream: No such file or directory in /hsphere/local/home/test/test/admin/resize.inc on line 3 Warning: imagesx(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 5 Warning: imagesy(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 6 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 15 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 16 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 19 Warning: Division by zero in /hsphere/local/home/test/test/admin/resize.inc on line 19 Warning: imagecreatetruecolor(): Invalid image dimensions in /hsphere/local/home/test/test/admin/resize.inc on line 23 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 24 Warning: imagejpeg(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 25 Warning: imagedestroy(): supplied argument is not a valid Image resource in /hsphere/local/home/test/test/admin/resize.inc on line 26 Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/test/test.ie/admin/resize.inc:34) in /hsphere/local/home/test/test.ie/admin/add_product2.php on line 113 
    


  • Closed Accounts Posts: 975 ✭✭✭squibs


    When working with file paths why not use
    $target_path = 
    $_SERVER['DOCUMENT_ROOT']."/images/prod_images/main/testfile.txt"
    

    to make sure the path is right? Makes the code more portable too.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Btw this is how Im calling the function in the form
    	if($image1 != ""){
    		exec("cp $image1 '$sitedir/images/prod_images/main/$id-1.jpg'");
    		exec("mv $image1 '$sitedir/images/prod_images/$id-1.jpg'");
    		cropImage(220, 160, '/hsphere/local/home/test/test/images/prod_images/main/'.$id.'-1.jpg', 'jpg', '/hsphere/local/home/test/test/images/prod_images/main/'.$id.'-1.jpg');
    		cropImage(220, 160, '/hsphere/local/home/test/test/images/prod_images/'.$id.'-1.jpg', 'jpg', '/hsphere/local/home/test/test./images/prod_images/'.$id.'-1.jpg');
    		resizethumb($id, 1, $thumbfolder, $thumbwidth, $thumbheight);
    	     resizemain($id, 1, $mainfolder, $mainwidth, $mainheight);
    	}
    	Header("Location: done.php");
    }
    


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    megcork wrote: »
    Btw this is how Im calling the function in the form
    	if($image1 != ""){
    		exec("cp $image1 '$sitedir/images/prod_images/main/$id-1.jpg'");
    		exec("mv $image1 '$sitedir/images/prod_images/$id-1.jpg'");
    		cropImage(220, 160, '/hsphere/local/home/test/test/images/prod_images/main/'.$id.'-1.jpg', 'jpg', '/hsphere/local/home/test/test/images/prod_images/main/'.$id.'-1.jpg');
    		cropImage(220, 160, '/hsphere/local/home/test/test/images/prod_images/'.$id.'-1.jpg', 'jpg', '/hsphere/local/home/test/test./images/prod_images/'.$id.'-1.jpg');
    		resizethumb($id, 1, $thumbfolder, $thumbwidth, $thumbheight);
    	     resizemain($id, 1, $mainfolder, $mainwidth, $mainheight);
    	}
    	Header("Location: done.php");
    }
    
    That code isn't very portablel :( - - won't work on a windows machine. You are using the CLI to copy, using linux only functions such as cp and mv.

    You should use the php copy function. Just something id advise :)


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Hey i moved it to a different testing account and its working fine. Woohoo. Thanks for all your help.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Good work :)


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    AARRRGH wrote: »
    Could you try this?

    [php]
    cropImage(220, 160, '/images/prod_images/main/'.$id-1.jpg.'', 'jpg', '/images/prod_images/main/'.$id-1.jpg.'');
    [/php]

    Just wondering how i could write the above with a variable '$k' substituting the '1' of the jpg?

    eg: I tried writing it as
    [php]
    cropImage(220, 160, '/images/prod_images/main/'.$id'.'-'.$k.jpg'', 'jpg', '/images/prod_images/main/'.$id'.'-'.$k.jpg.'');
    [/php]
    but I get errors


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Try this, you have your .'s in the wrong places.

    [php]cropImage(220, 160, '/images/prod_images/main/'.$id.'-'.$k.'.jpg', 'jpg', '/images/prod_images/main/'.$id.'-'.$k.'.jpg'); [/php]


  • Registered Users, Registered Users 2 Posts: 788 ✭✭✭sleepyescapade


    Brilliant :D


Advertisement