I'm developing a custom image upload field in WordPress, but I'm having a ton of difficulty dealing with the image once it's been uploaded. In addition to the upload, I need to resize the image to use a thumbnail. Every time I try to work with the uploaded image, I get an error to do with being unable to find the file (even though I can view it in a browser and it's clearly in the directory). Images are 666 by default when uploaded, but I've also tried manipulating one at 777 with the same results. The resize function is called on its own after the image is uploaded. Here's one of the attempts I made:
function resize_author_picture($filename) {
$filename = $_POST['file'];
$file = fopen($filename, 'r');
$data = fread($file);
fclose($file);
$dimensions = getimagesize($filename);
$dir = dirname($filename);
$crop = wp_crop_image( $data, $dimensions[0], $dimensions[1], 0, 0, 250, 280, null, $dir."/image.jpg" );
die( "crop: ".var_dump($crop)." file: ".$filename." path: ".$dir."/image.jpg" );
}
Here I used fopen() as a second attempt once providing just the image's path didn't work. Here is the previous attempt:
function resize_author_picture($file) {
$file = $_POST['file'];
$dimensions = getimagesize($file开发者_StackOverflow中文版);
$dir = dirname($file);
$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280, null, $dir."/image.jpg" );
die( "crop: ".var_dump($crop)." file: ".$file." path: ".$dir."/image.jpg" );
}
Both return a WP error object along these lines:
string(123) "File <http://site.local/wp-content/uploads/2010/09/squares-wide.jpeg> doesn't exist?"
Running out of ideas, any input is appreciated!
Doubt you still need an answer, as this question is quite old, but here it is for future reference:
The error your getting is a return of wp_load_image
which is used by wp_crop_image
. wp_load_image
uses the php function file_exists
, which needs to be fed the file's path without the domain.
So
$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280,
null, "wp-content/uploads/2010/09/squares-wide.jpeg" );
would have worked.
An aside wp_upload_bits
does not only upload the file(s) for you it also returns the URL of the uploaded file.
If you call wp_upload_bits
like so (where "file" is the name of the form's input):
if ($_FILES["file"]["name"]!="") {
$uploaded_file = wp_upload_bits($_FILES["file"]["name"], null,
file_get_contents($_FILES["file"]["tmp_name"]));
}
Hence $uploaded_file['url']
is equivalent to $dir."/image.jpg"
. In the above crop, you could have used a substring of $uploaded_file['url']
.
Concrete example:
With http://site.local/wp-content/uploads/2010/09/squares-wide.jpeg
this will work:
$dir = dirname($file);
$dir_substr = substr($dir, 18)
$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280,
null, $dir."/squares-wide.jpeg" );
Of course you also want the filename to be dynamic, so I'd call wp_uload_bits
as proposed above (if it's not coming from a form field, but a WP Custom field call it as you are doing now, the important part is $uploaded_file = wp_upload_bits(...)
to save the return of wp_upload_bits
in a variable for later use) and then do
$file_uri = substr($uploaded_file['url'], 18);
$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280,
null, $file_uri );
If you used the inline upload feature, your picture will be in the /wp-content/uploads folder, unless you've specified another folder on the Miscellaneous admin panel.
Be sure that you have not changed the upload directory location.
Try uploading using the WP Post tools to ensure that your settings are correct. Then move on the debugging code - once you ruled out the most basic.
精彩评论