Jump to content

Recommended Posts

Hello,

 

I'm trying to run this script on my site (it worked fine with GoDaddy and Dreamhost shared servers, but not my dedicated server I'm using now):

 

<?php if ($_FILES['uploaded2']['tmp_name'] == NULL) {

}

else {


// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploaded2']['tmp_name'];

echo $uploadedfile;
echo "<br/>";
echo "<br/>";
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

echo $src;

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=150;
$newheight=($height/$width)*150;
$tmp=imagecreatetruecolor($newwidth,$newheight);



// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "games_images/thumbs/".$pictureNum."_".$_FILES['uploaded2']['name'];
imagejpeg($tmp,$filename,100);

echo $filename;

imagedestroy($src);
imagedestroy($tmp);

// NOTE: PHP will clean up the temp file it created when the request
// has completed.
}
?>

 

For some reason, when I run:

 

$src = imagecreatefromjpeg($uploadedfile);

 

echo $src;

 

Nothing is echoed back from $src. For some reason, imagecreatefromjpeg() is not returning any information to be assigned to the $src variable. However:

 

echo $uploadedfile;

 

Returns the temporary file name from the upload field from the previous page.

 

I recompiled Apache in WHM with GD selected, and GD shows up as enabled in my phpinfo() (viewable at http://www.gameargus.com/test.php), but this function still doesn't seem to be working.

 

As I said, it worked fine on Godaddy and Dreamhost, so there has to be something setup incorrectly here on the dedicated server.

Link to comment
https://forums.phpfreaks.com/topic/70667-problem-with-imagecreatefromjpeg/
Share on other sites

Try running:

 

<?php

$src = imagecreatefromjpeg($uploadedfile);
var_dump($src);

?>

 

The information it will give might be helpful. Also turn error reporting to all by writing in the beginning:

error_reporting(E_ALL);

Maybe you will get a warning that will explain everything.

 

 

Orio.

Here is the information I get when I run those commands:

 

var_dump($src) returns: bool(false)

 

error_reporting(E_ALL) returns:

 

Warning: imagecreatefromjpeg(/tmp/phpprDvtk) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 311

bool(false)

Warning: getimagesize(/tmp/phpprDvtk) [function.getimagesize]: failed to open stream: No such file or directory in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 315

 

Warning: Division by zero in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 323

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 324

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 330

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 335

games_images/thumbs/29_commentslogo.jpg

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 339

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 340

 

How do I go about coding that? I have no idea what I'm doing here...I'm taking a file I've uploaded and running a script to convert it to a thumbnail; however, it is basically telling me that the /tmp folder foesn't exist, even though it does.

Create a folder called tmp_imgs in the folder with the script and do it this way: (The part between the *** is my addition)

 

<?php if ($_FILES['uploaded2']['tmp_name'] == NULL) {

}

else {


// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploaded2']['tmp_name'];

echo $uploadedfile;
echo "<br/>";
echo "<br/>";

//********************************************
//Move file to temp folder, using some random filename
$tmp_name = "tmp_imgs/".time().rand(0,1000).".jpg";
move_uploaded_file($_FILES['uploaded2']['tmp_name'], $tmp_name);
$uploadedfile = $tmp_name;
//*******************************************

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
unlink($tmp_name); //*******I added this too***** Delete the tmp file

echo $src;

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=150;
$newheight=($height/$width)*150;
$tmp=imagecreatetruecolor($newwidth,$newheight);



// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "games_images/thumbs/".$pictureNum."_".$_FILES['uploaded2']['name'];
imagejpeg($tmp,$filename,100);

echo $filename;

imagedestroy($src);
imagedestroy($tmp);

// NOTE: PHP will clean up the temp file it created when the request
// has completed.
}
?>

 

 

Orio.

 

Now I'm getting this:

 

 

tmp_imgs/1190757401460.jpg

Warning: imagecreatefromjpeg(tmp_imgs/1190757401460.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 322

bool(false)

Warning: getimagesize(tmp_imgs/1190757401460.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/gameargu/public_html/cms/htms/admin-games-image-uploaded.php on line 326

 

I used the code you suggested and CHMOD 777 the tmg_imgs directory...

 

<?php if ($_FILES['uploaded2']['tmp_name'] == NULL) {

}

else {
error_reporting(E_ALL);

// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploaded2']['tmp_name'];

echo "<br/>";
echo "<br/>";


//********************************************
//Move file to temp folder, using some random filename
$tmp_name = "tmp_imgs/".time().rand(0,1000).".jpg";
move_uploaded_file($_FILES['uploaded2']['tmp_name'], $tmp_name);
$uploadedfile = $tmp_name;
//*******************************************

echo $uploadedfile;

I can confirm that the directory exists and also that it is writable.

 

I used:

 

  <?php
$target = "tmp_imgs/";
$target = $target .$pictureNum."_banner_". basename( $_FILES['uploaded2']['name']) ;
$ok=1;




if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}




if ($ok==0)
{
Echo "Sorry your file was not uploaded";
} 


else
{ 


if(move_uploaded_file($_FILES['uploaded2']['tmp_name'], $target))
{
chmod($target, 0777);

}
else {
echo "Sorry, there was a problem uploading your file.";
}


}



?>

 

and the file was correctly placed in this directory.

 

The only problem seems to be using the imagecreatefromjpeg function. I can save the files directly to the sever, but when I need to resize them using imagecreatefromjpeg its giving me these errors

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.