I have this script:
function createThumb($source, $thumb_width=100)
{
$fl = dirname($source).'<br>';
$new_name = 'thumb_'.basename($source);
$img = imagecreatefromjpeg($source);
$width = imagesx($img);
$height = imagesy($img);
$new_width = $thumb_width;
$new_heght = floor($height * ($thumb_width / $width));
$tmp_name = imagecreatetruecolor( $new_width, $new_heght );
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_heght, $width, $height);
imagejpeg($tmp_img, $fl.DIRECTORY_SEPARATOR.$new_name);
}
All data works fine. I echo every step to imagecopyresized where I get this warning.
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /www/mdkbg.com/keasport/root/admin/parsing_vars.php5 on line 41
what could be the problem? I've changed the folder permission to 755 and I use php5 file tipes.
$tmp_name = imagecreatetruecolor( $new_width, $new_heght );
should read:
$tmp_img = imagecreatetruecolor( $new_width, $new_heght );
Related
I'm creating a function to crop images using PHP GD.
The function works perfectly for JPEG images but when I try to use the same function for PNG I just get a blank page and the following errors when I inspect the page in chrome:
I realize this is a javascript error, but this is probably something chrome does? This is what happens when i visit the IMAGE URL. If i replicate this with the JPEG code it works perfectly.
Edit: in Firefox it says "The image [URL] cannot be displayed because it contains errors. The below js error is just a chrome error and isn't very relevant.
Uncaught TypeError: Cannot read property 'getAttribute' of null
data_loader.js:2 Uncaught TypeError: Cannot read property
'hasAttribute' of null global-shortcut.js:9
The working JPEG code is as follows:
$fileName = $_POST['file'];
$jpeg_quality = 100;
$ratio = $_POST['r'];
$src = '/var/www/admin/public_html/images/'.$fileName;
$image = imagecreatefromjpeg($src);
$target = '/var/www/admin/public_html/images/crop/'.$fileName;
$thumb_width = $_POST['w'] / $ratio;
$thumb_height = $_POST['h'] / $ratio;
$width = imagesx($image);
$height = imagesy($image);
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0,
0,
$_POST['x'] / $ratio, $_POST['y'] / $ratio,
$width, $height,
$width, $height);
imagejpeg($thumb, $target, $jpeg_quality);
The Broken PNG code is as follows:
$fileName = $_POST['file'];
$jpeg_quality = 100;
$ratio = $_POST['r'];
$src = '/var/www/admin/public_html/images/'.$fileName;
$image = imagecreatefrompng($src);
$target = '/var/www/admin/public_html/images/crop/'.$fileName;
$thumb_width = $_POST['w'] / $ratio;
$thumb_height = $_POST['h'] / $ratio;
$width = imagesx($image);
$height = imagesy($image);
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0,
0,
$_POST['x'] / $ratio, $_POST['y'] / $ratio,
$width, $height,
$width, $height);
imagepng($thumb, $target, $jpeg_quality);
I answered my own question.
imagepng() function takes quality argument from 0 to 9
imagejpeg() function takes quality argument from 0 to 100
I have writen a script in PHP to upload an image.
To the point, my goal is to upload and to send 2 images to the server, 1 the original and 1 is the thumbnail. My script works, but is not perfect.
This is my script
<?php
//this is script for get data type file
$acak = rand(000000,999999);// for random
$lokasi_file = $_FILES['fupload']['tmp_name'];
$nama_file = $_FILES['fupload']['name'];
$nama_file_acak = $acak.$nama_file;
$ukuran_file = $_FILES['fupload']['size'];
$tipe_file = $_FILES['fupload']['type'];
$direktori = "fkendaraan/$nama_file_acak";
$uplod = move_uploaded_file($lokasi_file,"$direktori"); //to move image from local to the server folder
//to handle uplod thumbnail image
$img = imagecreatefromjpeg($direktori);
$width = imagesx($img);
$height = imagesy($img);
$new_width = 200;
$new_height = ($new_width/$width) * $height;
$tmp_img = imagecreatetruecolor( $width, $height );
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
//imagecopyresized( $tmp_img, $img, 200, 200, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, $direktori."thumb-".$nama_file_acak );
imagedestroy($tmp_img);
imagedestroy($direktori);
//---------------------------------------------------------------
//I have no Problem with query and database, it works fine
$sql = "";
$query = mysql_query($sql);
?>
This can run but not perfect because the result like this
Any one can help me to fix this? Im very nubie in php
Try changing this:
$tmp_img = imagecreatetruecolor( $width, $height );
To this:
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
Anyway I would recommend you to make use of some classes for these tasks such as:
Shiege Iseng Resize Class.
But of course, if you are trying to learn with this, that's ok :)
Is it possible to delete the images that imagejpeg creates I have the images uploading to my Amazon S3 server but the files just pop up in the main directory on my server after its ran.
$new_height = 120;
$width = imagesx($originalImage);
$height = imagesy($originalImage);
$new_width = round(($width * $new_height) / $height);
$imageResized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($imageResized, $originalImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$tmp_loc = 'uploads/thumb/';
$tempfilename = tempnam($tmp_loc, $filename);
imagejpeg($imageResized, $filename,100);
imagedestroy($imageResized);
imagedestroy($originalImage);
unlink($tempfilename);
I tried imagedestroy and unlink($tempfilename); but the file remains.
imagejpeg(...) should be outputting to $tempfilename rather than $filename, then you'd be unlinking the right file.
You forget to open the variable $tmp_loc.
Look:
$tmp_loc = uploads/thumb/';
Correct:
$tmp_loc = 'uploads/thumb/';
Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.
I have this script:
if(($_POST['number2'] && !$_POST['button_add']) || ($_POST['number2'] && !$_POST['button_add_gal'])) { $num=$_POST['number2'];
for($p=0; $p<$num; $p++) {
if ($_POST['page']=='news') {
$dir = '../images/news/'; // Директорията в която ще се записват файловете
}
if ($_POST['page']=='gallery') {
$dir = '../images/gallery/'; // Директорията в която ще се записват файловете
}
$name[$p]='gal_'.$_FILES['file']['name'][$p];
move_uploaded_file($_FILES['file']['tmp_name'][$p], $dir.$name[$p]);
$filename[$p] = $name[$p];
if ($_POST['page']=='news') {
createThumb('../images'.DIRECTORY_SEPARATOR.'news'.DIRECTORY_SEPARATOR.$filename[$p]);
echo '<img src="../images/news/thumb_'.$filename[$p].'" width="50" height="50" border="0" style="margin-left:10px;">';
}
if ($_POST['page']=='gallery') {
createThumb('../images'.DIRECTORY_SEPARATOR.'gallery'.DIRECTORY_SEPARATOR.$filename[$p]);
echo '<img src="../images/gallery/thumb_'.$filename[$p].'" width="50" height="50" border="0" style="margin-left:10px;">';
if($_POST['page']=='gallery'){
resizeImage('../images'.DIRECTORY_SEPARATOR.'gallery'.DIRECTORY_SEPARATOR.$filename[$p]); }
if ($_POST['page']=='news'){
resizeImage('../images'.DIRECTORY_SEPARATOR.'news'.DIRECTORY_SEPARATOR.$filename[$p]);
}
}
} }
function createThumb($source, $thumb_width=150)
{
$fl = dirname($source);
$new_name = 'thumb_'.basename($source);
$img = imagecreatefromjpeg($source);
$width = imagesx($img);
$height = imagesy($img);
$new_width = $thumb_width;
$new_heght = floor($height * ($thumb_width / $width));
$tmp_img = imagecreatetruecolor( $new_width, $new_heght );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_heght, $width, $height);
imagejpeg($tmp_img, $fl.DIRECTORY_SEPARATOR.$new_name);
}
function resizeImage($source, $thumb_width=700)
{
$fl = dirname($source);
$new_name = basename($source);
$img = imagecreatefromjpeg($source);
$width = imagesx($img);
$height = imagesy($img);
$new_width = $thumb_width;
$new_heght = floor($height * ($thumb_width / $width));
$tmp_img = imagecreatetruecolor( $new_width, $new_heght );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_heght, $width, $height);
imagejpeg($tmp_img, $fl.DIRECTORY_SEPARATOR.$new_name);
}
It works fine with small pictures but if I use bigger pictures it works only for 2 files. If I attach 3 or more files it upload them and when the page is refreshed, expecting to see the uploaded pictures there is nothing on the page. It's returned into default state. Not even an error message is displayed. I reconfigured the php5.ini upload_max_filesize to 100M but still nothing.I use php5 file extensions and safe_mode is switched off to php5 with CGI mode and gd2 is active. What could be the problem?
I wonder if your script is timing out? Add ini_set("max_execution_time", 500); or something to the top of the script and see if that helps