Voting

: four plus zero?
(Example: nine)

The Note You're Voting On

tomas at dundacek dot cz
15 years ago
I had a problem with denied permissions when trying to upload AND resize an image having safe_mode on. This caused that I couldn't create the new file in which I wanted to resampled the image with nor with imagejpeg() nor with touch() and imagejpeg() after it.

Here is my solution, I didn't test, but it's possible, it is biting some memory:

<?php
function resize($image, $target_file) {
// $image is the uploaded image
list($width, $height) = getimagesize($image['tmp_name']);

//setup the new size of the image
$ratio = $width/$height;
$new_height = 500;
$new_width = $new_height * $ratio;

//move the file in the new location
move_uploaded_file($image['tmp_name'], $target_file);

// resample the image
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg($target_file);
imagecopyresampled($new_image,$old_image,0,0,0,0,$new_width, $new_height, $width, $height);

//output
imagejpeg($new_image, $target_file, 100);
}

?>

As you can see, the function moves the uploaded file where you want to save the resampled image (move_uploaded_file is not restricted by safe_mode) and then you can resample the image, because it was created by moving it already.

Note: the directory where you want to save the file must have permissions set to 0777.

<< Back to user notes page

To Top