Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
PHP guru's
Options
-
30-06-2008 9:11amHi all,
Been a while since I posted here! I've run in to a problem with a site I'm putting together, I've borrowed an image resize/upload script named simpleimage.php which I will post below, followed by the code being used and the problem.
simpleImage.php
[php]
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
[/php]implementation:
[php]
if (file_exists($directory)) {
$image = new SimpleImage();
$image->load($tmpName);
$image->resizeToWidth(450);
$image->save($directory.$newFile);
$image->resize(80,80);
$image->save($thumb_directory.$thumbFile);
$image->resizeToWidth(200);
$image->save($medium_directory.$mediumFile);
} else {
mkdir($directory, "777");
mkdir($thumb_directory, "777");
mkdir($medium_directory, "777");
$image = new SimpleImage();
$image->load($tmpName);
$image->resizeToWidth(450);
$image->save($directory.$newFile);
$image->resize(80,80);
$image->save($thumb_directory.$thumbFile);
$image->resizeToWidth(200);
$image->save($medium_directory.$mediumFile);
}
[/php]And the problem:
I had been using this initially without the medium sized images being created, and it was all working perfectly, but now that I've added the medium directory and tried to create the medium images I've been getting an error as follows:[B]Warning[/B]: imagejpeg() [[URL="http://localhost/newbeer/function.imagejpeg"]function.imagejpeg[/URL]]: Unable to open 'media/user_photos/user7/medium/' for writing: No such file or directory in [B]C:\xampp\htdocs\newbeer\inc\functions.php[/B] on line [B]258[/B]
Now the directory is there, the directories are set above as follows:
[php]$directory = 'media/user_photos/user'.$_SESSION.'/';
$thumb_directory = 'media/user_photos/user'.$_SESSION.'/thumbs/';
$medium_directory = 'media/user_photos/user'.$_SESSION.'/medium/';
[/php]And all get created sucessfully. I later added the chmod param to the mkdir() command thinking that was the problem but still no joy. I also just tried to bump up the memory limit to 50MB's, still nothing. Appreciate any light that could be shed on the matter, my brain isn't working0
Comments
-
Can you get the medium images to work in the directory above /medium?0
-
Nope, just tried setting the medium images directory to the same as the standard images directory (with a filename tweak so it didn't just overwrite) and it tells me that directory can't be written to, even though the standard images have just been written to it! :rolleyes:0
-
To rule out a path problem have you tried using absolute paths rather than relative ones?0
-
Yep, same problem and then some with absolute paths:
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/7-26851.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/thumbs/7-26851.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/medium/' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/7-736.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/thumbs/7-736.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/medium/' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/7-10649.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/thumbs/7-10649.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/medium/' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/7-1118.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/thumbs/7-1118.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/medium/' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/7-12351.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/thumbs/7-12351.jpeg' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost/newbeer/media/user_photos/user7/medium/' for writing: Invalid argument in C:\xampp\htdocs\newbeer\inc\functions.php on line 258
0 -
I didn't mean to use absolute URLs but absolute file system paths to the directories in question.0
-
Advertisement
-
The absolute paths should be something like:
/xampp/htdocs/newbeer/media/user_photos/user7/thumbs/
It seems that you have "http://localhost" in the paths.
What happens when you disable the creation of the first images (i.e. comment out the resize/save to $directory [PHP] $image = new SimpleImage();
$image->load($tmpName);
//$image->resizeToWidth(450);
//$image->save($directory.$newFile);
//$image->resize(80,80);
//$image->save($thumb_directory.$thumbFile);
$image->resizeToWidth(200);
$image->save($medium_directory.$mediumFile);[/PHP] Also, it would probably be best to create the medium images first - right now you are resizing to 450, then down to 80 and then back up to 250. The medium image is going to look pixelated.0 -
This is windows yeah?
Do you know if apache is running under a specific account or as the SYSTEM user?0 -
I didn't mean to use absolute URLs but absolute file system paths to the directories in question.
Yeah sorryanswered below...
The absolute paths should be something like:
/xampp/htdocs/newbeer/media/user_photos/user7/thumbs/
It seems that you have "http://localhost" in the paths.
What happens when you disable the creation of the first images (i.e. comment out the resize/save to $directory [php] $image = new SimpleImage();
$image->load($tmpName);
//$image->resizeToWidth(450);
//$image->save($directory.$newFile);
//$image->resize(80,80);
//$image->save($thumb_directory.$thumbFile);
$image->resizeToWidth(200);
$image->save($medium_directory.$mediumFile);[/php] Also, it would probably be best to create the medium images first - right now you are resizing to 450, then down to 80 and then back up to 250. The medium image is going to look pixelated.
Tried this, the path '/xampp/htdocs/newbeer/media/user_photos/user7/' works in so far as it's correct, but still no joy with the medium images.
Then tried commenting out the rest, still nothing.This is windows yeah?
Do you know if apache is running under a specific account or as the SYSTEM user?
Yep, on XP, and under an account name ADAM which is an admin account, if that answers your question?0 -
Now that you've changed the path to an absolute path, what error do you get?0
-
In order to write images, you'll need to use the file path, not the URL. From the top post, you need
$directory = 'C:/xampp/htdocs/newbeer/media/user_photos/user'.$_SESSION['id'].'/'; $thumb_directory = 'C:/xampp/htdocs/newbeer/user'.$_SESSION['id'].'/thumbs/'; $medium_directory = 'C:/xampp/htdocs/newbeer/user'.$_SESSION['id'].'/medium/';
Although relative paths are supported, they are tricksy. I'd always recommend using an absolute path where feasible. Perhaps even maintain a $doc_root variable, e.g.$doc_root = "C:/xampp/htdocs/newbeer" $directory = $doc_root.'/media/user_photos/user'.$_SESSION['id'].'/';
so if/when you move the code to another machine, you only need to change this variable.
Note that in your mkdir() command, the "777" is superfluous. Windows doesn't recognise Unix permissions. So the ownership/permissions on the directories are not getting set.
Because apache and PHP are running as an Admin user, permissions shouldn't be an issue. Are you using XP pro or XP home?0 -
Advertisement
-
dzy wrote:Now that you've changed the path to an absolute path, what error do you get?
I get the following error:Warning: imagejpeg() [function.imagejpeg]: Unable to open '/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open '/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open '/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258
Just to be clear, here's the script I have now, with the SimpleImage class defined in an include:
[php]
<?php
include('inc/functions.php');
for($i=1; $i<6; $i++) {
if(isset($_POST) && $_FILES > 0) {
$fileName = $_FILES;
$tmpName = $_FILES;
$fileSize = $_FILES;
$fileType = $_FILES;
$rand=rand();
$directory = '/xampp/htdocs/newbeer/media/user_photos/user'.$_SESSION.'/';
$medium_directory = '/xampp/htdocs/newbeer/media/user_photos/user'.$_SESSION.'/medium/';
$thumb_directory = '/xampp/htdocs/newbeer/media/user_photos/user'.$_SESSION.'/thumbs/';
$newFile = $_SESSION.'-'.$rand.'.jpeg';
$thumbFile = $_SESSION.'-'.$rand.'.jpeg';
$meduimFile = $_SESSION.'-'.$rand.'.jpeg';
if (file_exists($directory)) {
$image = new SimpleImage();
$image->load($tmpName);
$image->resizeToWidth(450);
$image->save($directory.$newFile);
$image->resizeToWidth(200);
$image->save($medium_directory.$mediumFile);
$image->resize(80,80);
$image->save($thumb_directory.$thumbFile);
} else {
mkdir($directory, "0777");
mkdir($medium_directory, "0777");
mkdir($thumb_directory, "0777");
$image = new SimpleImage();
$image->load($tmpName);
$image->resizeToWidth(450);
$image->save($directory.$newFile);
$image->resizeToWidth(200);
$image->save($medium_directory.$mediumFile);
$image->resize(80,80);
$image->save($thumb_directory.$thumbFile);
}
}
}
[/php]
And the output is that the folders:
-'media/user_photos/user7/'
-'media/user_photos/user7/thumbs'
-'media/user_photos/user7/medium'
all get created, and the folders:
-'media/user_photos/user7/'
-'media/user_photos/user7/thumbs'
get pupulated with the images (the loop is for the upload form which allows for 1-5 images to be uploaded simultaneously) correctly i.e. 'media/user_photos/user7/' has the large images and 'media/user_photos/user7/thumbs' has the corresponding thumbnails. And then there's the medium folder, just sitting there. Empty...0 -
'/xampp/htdocs/newbeer/media/user_photos/user7/medium/' isn't an absolute Windows file system path. If xampp is at the root of the C drive, you need 'C:/xampp/htdocs/newbeer/media/user_photos/user7/medium/'0
-
In order to write images, you'll need to use the file path, not the URL. From the top post, you need
$directory = 'C:/xampp/htdocs/newbeer/media/user_photos/user'.$_SESSION['id'].'/'; $thumb_directory = 'C:/xampp/htdocs/newbeer/user'.$_SESSION['id'].'/thumbs/'; $medium_directory = 'C:/xampp/htdocs/newbeer/user'.$_SESSION['id'].'/medium/';
Although relative paths are supported, they are tricksy. I'd always recommend using an absolute path where feasible. Perhaps even maintain a $doc_root variable, e.g.$doc_root = "C:/xampp/htdocs/newbeer" $directory = $doc_root.'/media/user_photos/user'.$_SESSION['id'].'/';
so if/when you move the code to another machine, you only need to change this variable.
Note that in your mkdir() command, the "777" is superfluous. Windows doesn't recognise Unix permissions. So the ownership/permissions on the directories are not getting set.
Because apache and PHP are running as an Admin user, permissions shouldn't be an issue. Are you using XP pro or XP home?Warning: imagejpeg() [function.imagejpeg]: Unable to open 'C:/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'C:/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258 Warning: imagejpeg() [function.imagejpeg]: Unable to open 'C:/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258
I do have a $doc_root and $base_href set up for such scripting, neither have revealed anything about the problem.
Also, re the chmodding, I just threw that in there on account of the possibility of a unix server, as it doesn't do any harm in the event of a windows server. I didn't have that parameter in to begin with, I only added it after the errors as a possible solution, to no avail0 -
It might be worth (though you've probably already done it) looking at exactly what the functions.php script is doing at line 258. It might be adding in something odd.0
-
-
And right you are!
Thing is, the script I posted in my first post, simpleimage.php, is pasted in to functions.php and gets called from there. The oddity is that it's the only function called from functions.php and functions.php only has 222 lines of code...
[php]
<?
//$base_href = 'http://209.200.237.111/newbeer';
$base_href = 'http://localhost/newbeer';
global $base_href;
include('./db/db_conn.php');
session_start();
function dob() {
$day='<select name="day">';
for($i=1; $i<32; $i++) {
$day.='<option value='.$i.'>'.$i.'</option>';
}
$day.='</select>';
$month='<select name="month">';
for($i=1; $i<13; $i++) {
$month.='<option value='.$i.'>'.$i.'</option>';
}
$month.='</select>';
$year='<select name="year">';
for($i=1901; $i<2008; $i++) {
$year.='<option value='.$i.'>'.$i.'</option>';
}
$year.='</select>';
$dob = $day.$month.$year;
return $dob;
}
function countries() {
$sql="SELECT * FROM countries ORDER BY Country";
$result=mysql_query($sql) or die(mysql_error());
$countries='<select name="countries">';
while($row=mysql_fetch_array($result)) {
$countries.='<option value="'.$row.'">'.$row.'</option>';
}
$countries.='</select>';
return $countries;
}
function checkEmail($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
function get_relationship_select() {
$sql="SELECT * FROM tbl_relationship";
$result=mysql_query($sql) or die(mysql_error());
$sql2="SELECT user_relationship FROM tbl_user_personal WHERE user_id={$_SESSION} LIMIT 1";
$result2=mysql_query($sql2) or die(mysql_error());
$row2=mysql_fetch_array($result2);
$row2=$row2;
$relationships='<select name="relationship">';
while($row=mysql_fetch_array($result)) {
if($row == $row2) {$check = " SELECTED";}
$relationships.='<option value="'.$row.'"'.$check.'>'.$row.'</option>';
$check = '';
}
$relationships.='</select>';
return $relationships;
}
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
function get_profile_photo($user_id) {
$sql="SELECT * FROM tbl_users INNER JOIN tbl_user_personal ON tbl_users.user_id=tbl_user_personal.user_id WHERE tbl_users.user_id = $user_id LIMIT 1";
$result=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_array($result);
if($row == '') {
$profile_photo = '<img src="media/images/man_medium.gif" class="profile_thumb">';
} else {
$profile_photo = '<img src="media/user_photos/user'.$user_id.'/thumbs/'.$row.'" class="profile_thumb" />';
}
return $profile_photo;
}
function get_featured_members() {
$sql="SELECT * FROM tbl_users INNER JOIN tbl_user_personal ON tbl_users.user_id = tbl_user_personal.user_id LIMIT 4";
$result=mysql_query($sql) or die(mysql_error());
$i=1;
$featured_members.='<table><tr>';
while($row=mysql_fetch_array($result)) {
$profile_photo = get_profile_photo($row);
$featured_members.='<td><a href="profile.php?MemberID='.$row.'">'.$profile_photo.'</a></td>';
if($i % 2 == 0) {
$featured_members.='</tr><tr>';
}
}
$featured_members.='</tr></table>';
return $featured_members;
}
?>
[/php]0 -
$meduimFile0
-
-
Does that make me a guru?0
-
-
Advertisement
-
$meduimFile
php.exe -l functions.php
It does a syntax/lint check on the file. IIRC it reports uninitialised variables.
There was a clue in the error message - there wasn't a filename listed.Warning: imagejpeg() [function.imagejpeg]: Unable to open 'C:/xampp/htdocs/newbeer/media/user_photos/user7/medium/' for writing: No such file or directory in C:\xampp\htdocs\newbeer\inc\functions.php on line 258
Yes, I know, I didn't spot either.0 -
Cheers for that hint daymobrew, I'll bookmark that!0
-
Hands up how many coders suffer the error of a spelling mistake!0
Advertisement