SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Gurus: Problem with imagejpeg() due to safe mode?

    Hello,

    The following script used to work fine. I hadn't used it for a long time and in the meantime I guess, things changed with php (safe mode? etc.).

    I get the following error now when trying to scale an image:

    Warning: imagejpeg() [function.imagejpeg]: Unable to open '/home/domain/public_html/fileresize/temp/18d7011550ad5fa215d9e089a5aa27aa-image.jpeg' for writing in /home/domain/public_html/fileresize/resizejpg.php on line 90

    line 90 is this:
    imagejpeg($image, $base_fs . '-image.jpeg');

    I searched and found recommendations to use touch();

    touch($newfile);
    imagejpeg($image_p, $newfile, 100);


    I tried that and it still doesn't work. So I don't know if I'm putting touch(); in the wrong place, or what else might be wrong. I'm really hoping someone can help get this working again. It's for a non-profit animal rescue website and having the image resize is vital to what I need to accomplish.

    Another "fix" I found when searching is this, but I don't really know what to do with that either:

    the workaround however is if you create the file using some other function, before call imagejpeg(). something like this

    <?
    $fh=fopen($file_name_dest,'w');
    fclose($fh);
    imagejpeg($dest,$file_name_dest, $quality);
    ?>


    I haven't worked with php much for over two years and I wasn't a guru then though I tried. I'm hoping someone can tell me where/how to implement either of the two fixes, or another way to get this script to work with the new server (gd?) configuration. Please...

    Here's the script/form that worked prior to the security change:

    Code:
    <?php
    
    	/*
    	 * php+gd image scaling and overlay demo.
    	 * copyright 2003, B. Johannessen <bob@db.org>
    	 *
    	 * This program is free software; you can redistribute it and/or modify
    	 * it under the terms of the GNU General Public License as published by
    	 * the Free Software Foundation; either version 2 of the License, or
    	 * (at your option) any later version, and provided that the above
    	 * copyright and permission notice is included with all distributed
    	 * copies of this or derived software.
    	 *
    	 * This program is distributed in the hope that it will be useful,
    	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    	 * GNU General Public License for more details.
    	 *
    	 * A copy of the GNU General Public License is available from the GNU
    	 * website at the following URL: http://www.gnu.org/licenses/gpl.txt
    	 */
    
    	/* 
    	 * configuration: sets the full path of the overlay image,
    	 * the rgb value to make transparant in the overlay image,
    	 * the path to store the scaled images and the uri prefix
    	 * for the scaled images.
    	 */
    
    	$cache_fs = '/home/domain/public_html/fileresize/temp/';
    	$cache_uri = 'http://www.domain.org/fileresize/temp/';
    
    	/*
    	 * calculate the filesystem and uri prefixes based on a semi-random string.
    	 */
    	$srid = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['REMOTE_PORT'] . time());
    	$base_fs = $cache_fs . $srid;
    	$base_uri = $cache_uri . $srid;
    
    	if($_SERVER['PATH_INFO'] == '/source') {
    		$source = str_replace(' ', ' ', htmlspecialchars(implode('', file('resizejpg.php'))));
    		$show = 'source';
    	}
    
    	else if(isset($_FILES['file'])) {
    		/*
    		 * open original (uploaded) image, based on type.
    		 */
    		switch($_FILES['file']['type']) {
    			case 'image/jpeg':
    			case 'image/pjpeg':
    				$orig = imagecreatefromjpeg($_FILES['file']['tmp_name']);
    				break;
    			case 'image/png':
    				$orig = imagecreatefrompng($_FILES['file']['tmp_name']);
    				break;
    			case 'image/gif':
    				$orig = imagecreatefromgif($_FILES['file']['tmp_name']);
    				break;
    			default:
    				$error = 'Unknown File Format or MIME Type';
    				$show = 'error';
    		}
    		if($orig) {
    			/*
    			 * fetch the size of the original image,
    			 * and calculate the size of the new image and thumb.
    			 */
    			$orig_x = imagesx($orig);
    			$orig_y = imagesy($orig);
    			$image_x = $_REQUEST['image_x'];
    			$image_y = round(($orig_y * $image_x) / $orig_x);
    
    			/*
    			 * create the new image, and scale the original into it.
    			 */
    			$image = imagecreatetruecolor($image_x, $image_y);
    			imagecopyresampled($image, $orig, 0, 0, 0, 0, $image_x, $image_y, $orig_x, $orig_y);
    
    
    			// write the jpg to disk.
    // here is where i added touch($image);
    // but it didn't work. Did I do it wrong?
    
    			imagejpeg($image, $base_fs . '-image.jpeg');
    
    			// calculate the uri of the 3 images.
    
    			$image_uri = $base_uri . '-image.jpeg';
    		}
    		else {
    			if(!$error)
    				$error = 'Error reading uploaded image';
    			$show = 'error';
    		}
    	
    		if(!isset($show))
    			$show = 'result';
    	}
    
    	$title = 'Scale Image Utility';
    
    ?>
    
            <?php if($show == 'error') { ?>
              <h2><?=$error?></h2>
            <?php } else if($show == 'source') { ?>
              <h2>Source</h2>
              <pre class="php-source"><?=$source?></pre>
            <?php } else if($show == 'result') { ?>
              <h2>Scaled Image</h2>
              <p><img src="<?=$image_uri?>"/></p>
    	<?php } else { ?>
              <h2>Resize</h2>
              <?php } ?>
              <table width="600" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td><form enctype="multipart/form-data" action="http://www.domain.org/fileresize/resizejpg.php" method="post">
                    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
                    <table>
                      <tr>
                        <td>Width:</td>
                        <td><input name="image_x" type="text" size="10">
            pixels</td>
                      </tr>
                      <tr>
                        <td>Image File:</td>
                        <td><input name="file" type="file">
                        </td>
                      </tr>
                      <tr>
                        <td><input name="submit" type="submit" value="Scale">
                        </td>
                        <td></td>
                      </tr>
                    </table>
                    <p>Instructions: In the width field, select the new width you
                      would like your image to be. 'Browse' to the image on your
                      hard drive, then select 'Scale.' Be patient while the image
                      is scaled. When the process is complete, the scaled image will
                      be viewable on the page. Put your mouse over the scaled image,
                      right-click and select 'Save Picture As....' Save it to your
                      desktop, then you will be able to upload the image.... (The resized
                      image will have a long file name; be sure to rename it to its
                      original name.)</p>
                  </form></td>

    Thanks for any help with this...

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Do you have write access to the directory you're saving in?

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think so if you mean chmod... permissions are 777.

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've only ever had problems with imagejpeg when the file already existed, never when it didn't. Try making sure the file doesn't exist before you output your image, see if that helps. (Your PHP script may for some reason not have write access to the file.)
    PHP questions? RTFM
    MySQL questions? RTFM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •