Jump to content

problem with imagejpeg() due to safe mode?


kdreg

Recommended Posts

Hello,

 

The following script used to work great. 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

 

I searched and found recommendations to use touch();

 

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.

 

Thanks.

 

Here's the full code:

 

<html>

<head>

<title>Image resize</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

<?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.
[b]
[color=red]// here is where i added touch($image);
// but it didn't work. Did I do it wrong?[/color][/b]

		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>

           

Link to comment
Share on other sites

another workaround I found in the php.net info is the following, but I'm not sure how to implement that either. Please, any help for a newbie? I haven't worked with the script for about 3 years and at that time I understood things better. But I'm lost with this security change. Surely a lot of people were affected when it happened.

 

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); 

?>

Link to comment
Share on other sites

i will give you a script so be sure to implement on it..

you should be able to read what's going on and how to work with it..

 

<?php
MakeItFit($save,$reSize){
$tmpPath=$_FILES['filefield']['tmp_name'];
$picName=$_FILES['filefield']['name'];
$filetype = $_FILES['filefield']['type'];
$size=$_FILES['filefield']['size'];
$datetime = date('m-d-y h:i:s');
//RENAME THE FOTO
$remove=strstr($picName,'.');
$rand = mt_rand(0, 32);
$number = md5($rand . time()).$remove;
//Image width and height
list($widthx,$heighty)=getimagesize($tmpPath);
//time of upload

//creating the make it fit picture for any width greater than the given $reSize
if($widthx>$reSize){
$srcImg=imagecreatefromjpeg("$tmpPath");
$ratio=$reSize/$widthx;
$thumbHeight=$heighty*$ratio;
$thumbImg=imagecreatetruecolor($reSize,$thumbHeight);
imagecopyresized($thumbImg,$srcImg,0,0,0,0,$reSize,$thumbHeight,imagesx($srcImg),imagesy($srcImg));
imagejpeg($thumbImg,"$save$number",100);
}elseif($widthx<$reSize){
$result=copy($tmpPath,"$save$number");
}

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.