I have a problem when uploading the image, I used a function to resize the image, my problem is that when I upload a small size image, It's resize successfully. But with a large size image the error cannot be found. I tried the following code:
if(move_uploaded_file($file_name,$folder.$file_name) && file_exists($folder.$file_name)){
$img = new imageLib($folder.$file_name);
$img->resizeImage(360, 360, 'portrait', true);
$img->saveImage($folder.'thumb-'.$file_name, 100);
echo "Uploaded";
}
else{
echo "Error";
}
I think the problem is that the image has not yet been uploaded but has run the resize function, so the error cannot be found.
Any help much appreciated! Thanks very much!
You Should Try:
$img = resize_image(‘/path/to/some/image.jpg’, 360, 360);
Related
I am using the following Imagick function to resize an image. The code runs and resizes the image, but with an error from the server saying "An error occurred while requesting the document from the testing server." And I cannot detect the problem. In the browser it, however, outputs non-human readable characters. In case I try to output the resized/modified image to the browser, there is no problem. I face this problem as I try to save the image to the disk.
Here is my code:
<?php
imagick_resize('running.jpg');
function imagick_resize($image, $width = 460, $height = 300)
{
// define widescreen dimensions
// $width = 460;
// $height = 300;
// load an image
$i = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/alchemyapi/' . $image);
// get the current image dimensions
$geo = $i->getImageGeometry();
// crop the image
if(($geo['width']/$width) < ($geo['height']/$height))
{
$i->cropImage($geo['width'], floor($height*$geo['width']/$width), 0, (($geo['height']-($height*$geo['width']/$width))/2));
}
else
{
$i->cropImage(ceil($width*$geo['height']/$height), $geo['height'], (($geo['width']-($width*$geo['height']/$height))/2), 0);
}
// thumbnail the image
$i->ThumbnailImage($width,$height,true);
// save or show or whatever the image
# $i->setImageFormat('png');
# header("Content-Type: image/png");
# unlink('small_square_img.png');
$i->writeImage($_SERVER['DOCUMENT_ROOT'] . '/alchemyapi/tmp/small_square_img.png');
# file_put_contents('small_square_img.png', $i);
exit($i);
}
?>
The data you are sending to the browser is not a valid image. It is quite likely that you have a Byte-Order-Marker in some of your files before the
You should check for that and fix it if it is occuring, or provide an example of the "non-human readable characters" for people to have a clue of what the problem is.
My attempt at getting the size of a newly uploaded image appears not to be working properly.
list($w, $h) = getimagesize($_FILES['image_field']);
echo 'width:'.$w;
outputs:
width:
How is this possible? I know for a fact the image (.jpg) is uploading fine because it's now on my server.
Use this code:
list($w, $h) = getimagesize($_FILES["image_field"]["tmp_name"]);
first you need to upload file then pass the uploaded location
getimagesize("location_of_file/".$file_name);
OR
getimagesize($path['path'].'/'.$_POST['name']);
OR
$file_name=$_FILES['fileToUpload']['name'];
getimagesize($target_dir.'/'.$file_name);
OR
$file_tmp=$_FILES['fileToUpload']['tmp_name']; list($width, $height) =
getimagesize($file_tmp);
$file_width=$width;
$file_height=$height;
I had asked this question and found out that the PHP configuration was limiting my uploads to 2MB, so I fixed it to 3MB. However, I have another problem now: I am also checking for image dimension and it is failing if the image appears to be over 3MB, throwing the below error. So how could I stop the error and check the size by myself instead of the PHP config?
Warning: getimagesize(): Filename cannot be empty
Here is the code:
$size = $_FILES['image']['size'];
list($width, $height) = getimagesize($_FILES['image']['tmp_name']);
$minh = 400;
$minw = 600;
$one_MB = 1024*1024; //1MB
if($size > ($one_MB*3))// this is not checking the size at all, the last echo is what I get if I try to upload over 3mb.
{
echo"File exceeds 3MB";
}
elseif ($width < $minw ) {
echo "Image width shouldn't be less than 600px";
}
elseif ($height < $minh){
echo "Image height shouldn't be less than 400px";
}
else{
if (move_uploaded_file($_FILES['image']['tmp_name'], $new_location)){
//do something
}
else{
echo "Image is too big, try uploading below 3MB";
}
}
Looks like the upload went wrong at some point. Try adding some error handling before getimagesize():
if (!file_exists($_FILES['image']['tmp_name'])) {
echo "File upload failed. ";
if (isset($_FILES['upfile']['error'])) {
echo "Error code: ".$_FILES['upfile']['error'];
}
exit;
}
It's hard to say why it has failed, as there are many parameters involved. The Error code might give you a hint.
Otherwise maybe start reading here: http://de2.php.net/manual/en/features.file-upload.php
I was also having the same error, go to php.ini and go to line 800 "upload_max_filesize".
Choose your max upload file size, i set mine to 4M and in my actual website code I dont allow anything after 3mb.
For some reason having them both the same size doesnt work, PHP.ini needs to be atleast 1mb bigger than what you allow to be uploaded.
I tried to make a thumbnail of a pdf file which is hosted on another server. My current code is:
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The problem is that code is only generating thumbnail for LAST PAGE of the pdf file. How can I make a thumbnail for first page only? I tried to add [0] at the imagick line.
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf[0]");
but it didn't work. It only work for local pdf file, i.e:
$im = new imagick("my-pdf-file.pdf[0]");
Please help me solve this problem.. Thanks..
You'll need to reset the active image to the first page. This can be done with Imagick::setIteratorIndex.
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setIteratorIndex(0);
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
Try...
$im->setImageIndex(0); //this will return 1th page of the pdf file
$im->setImageFormat('jpg');
"This can be done with Imagick::setIteratorIndex. .."
..or not. Simply has no effect . Setting it to one crashes something, setting it to 0 gets the last page..
function make_thumbnail($filename)
{
try
{
$imagick= new Imagick($filename);
}
catch(ImagickException $e)
{
// failed to make a thimbynail. what now?
// load up our trusty truetype font png instead?
$imagick->destroy();
return "0"; // shove any rubbish in the db - it will just say no image available when asked.
}
$imagick->setIteratorIndex(0);// rewind to first page or image of a multi series
$imagick->setImageFormat("png"); // turn it into a png
$imagick = $imagick->flattenImages(); // remove any transparency
$imagick->scaleImage(300,0); //resize...to less than 300px wide
$d = $imagick->getImageGeometry();
$h = $d['height'];
if($h > 300)
$imagick->scaleImage(0,300);
$imagick->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$imagick->setImageCompressionQuality(0);
$imagick->setIteratorIndex(0);
$a = $imagick->getImageBlob(); // output as bytestream
$imagick->destroy();
return $a;
}
Im trying to reduce the quality of uploaded image. Here is my code
$image_config['source_image'] = 'file.jpg';
$image_config['maintain_ratio'] = TRUE;
$image_config['quality'] = '30%';
$image_config['width'] = 1680;
$image_config['height'] = 1050;
$this->load->library('image_lib', $image_config);
$this->image_lib->resize();
The problem is so image has the same quality as the file.jpg etc it is not reduced.
I face same problem and I found something in image library file.
system/libraries/image_library.php line 455
if ($this->dynamic_output === FALSE)
{
if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
{
if ($this->source_image != $this->new_image)
{
if (#copy($this->full_src_path, $this->full_dst_path))
{
#chmod($this->full_dst_path, FILE_WRITE_MODE);
}
}
return TRUE;
}
}
This code ignore image rendering if width and height of destination image is the same as the source image.
Remove the above code and it should works.
Hey Nick, have you tried to display the errors?
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
Possible errors I can think of : not giving the correct path to the file or not having installed one of the 3 image libraries (GD/GD2, NetPBM or ImageMagick)
Hope this helps!
You just need need to give Numeric value not with "%" sign
Like $image_config['quality'] = '30';
Also check the real mime type. PNG files renamed with JPG extension cannot be reduced by quality.
$info = getimagesize($_filepath);
echo $info['mime'];