post - PHP $_FILES["fileToUpload"]["tmp_name"] -
how can turn $_files["filetoupload"]["tmp_name"]
variable used in move_uploaded_file?
this works:
$filename = compress_image(($_files["filetoupload"]["tmp_name"]), $url, 30);
but trying this:
$filename = compress_image($images, $url, 30);
but, when above not work.
one alternative starting on was:
file_put_contents($target_file , $image);
in case, image named directory properly, image broken.
to clarify:
need turn ($_files["filetoupload"]["tmp_name"]) variable result of
ob_start(); echo imagejpeg($image,null,30); $image =ob_get_clean(); ob_end_clean(); $image = addslashes($image);
i need use $image save directory. $image has been stored mysql. have tried encode, , decode on $image, still no luck.
let me explain problem step-by-step:
the mess starts @ line:
$image = imagecreatefromstring(file_get_contents($_files['filetoupload']['tmp_name']));
problems:
- never use temp file process; move uploaded file permanent place
move_uploaded_file()
first - use of
file_get_contents()
unnecessary; can useimagecreatefromjpeg()
(and other formats, gif, png, etc) replaceimagecreatefromstring(file_get_contents())
things
here compress_image()
function:
function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); } elseif ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); } imagejpeg($image, $destination_url, $quality); return $destination_url; }
problems:
- if mime none of above,
imagejpeg()
fail (but didn't cater it; check return value of function) - you didn't check
$destination_url
writable & exist or not
next, assumes compress_image()
works , returns $destination_url
valid jpeg created @ path, following codes cause further problems:
$sql = "insert fffdf (`user_id`,`imit`) values ('9','$image')"; if (mysqli_query($conn, $sql)) { echo "new record created successfully"; } else { echo "error: " . $sql . "<br>" . mysqli_error($conn); }
problems:
- why save
$image
directly db? it's bad practice store image data in db. save path instead whenever possible $image
inaccessible here; scope of$image
stays in functioncompress_image()
,$image
still contains value beforecompress_image()
, unless useglobal $image;
in compress function (which not suggested). pass$image
function parameters reference:function compress_image(&$image, $destination_url, $quality)
you don't need $source_url
if have image data stored in $image
.
hope above helps.
Comments
Post a Comment