php - Add watermark to file during upload -
i'm trying make upload function adds watermark every file uploaded in memory , saves files specific folder. however, can't watermark part of code work. know sure upload functions works without watermark code added it. believe error must watermark code "transfers" temporary file code moves file hard drive. how can fix problem?
the code follows:
foreach($_files['files']['tmp_name'] $key => $tmp_name ){ $file_name = $_files['files']['name'][$key]; $file_size =$_files['files']['size'][$key]; $file_tmp =$_files['files']['tmp_name'][$key]; //$file_type=$_files['files']['type'][$key]; //if($file_size > 2097152){ //$errors[]='file size must less 2 mb'; //} // echo '<img src="../images/watermark.gif">'; $q = "insert media (file_name, album_id, file_size) values ('$file_name','$album_id', '$file_size')"; $r = mysqli_query ($dbc, $q); $desired_dir = '../../customers/' . $customerfolder . '/'; // load watermark , photo $watermark = imagecreatefromgif('../images/watermark.gif'); $photo = imagecreatefromjpeg($file_tmp); // center watermark on photo $wx = imagesx($photo)/2 - imagesx($watermark)/2; $wy = imagesy($photo)/2 - imagesy($watermark)/2; imagecopy($photo, $watermark, $wx, $wy, 0, 0, imagesx($watermark), imagesy($watermark)); if(empty($errors)==true){ move_uploaded_file($photo,"$desired_dir".$file_name); //mysql_query($q); }else{ print_r($errors); } }
i came following solution:
// load stamp , photo apply watermark $stamp = imagecreatefromgif('../images/watermark.gif'); $marge_right = 10; $marge_bottom = 10; $sx = imagesx($stamp); $sy = imagesy($stamp); if(isset($_files['files'])){ $errors= array(); foreach($_files['files']['tmp_name'] $key => $tmp_name ){ $file_name = $_files['files']['name'][$key]; $file_size =$_files['files']['size'][$key]; $file_tmp =$_files['files']['tmp_name'][$key]; $file_type=$_files['files']['type'][$key]; //if($file_size > 2097152){ //$errors[]='file size must less 2 mb'; //} // echo '<img src="../images/watermark.gif">'; $q = "insert media (file_name, album_id, file_size) values ('$file_name','$album_id', '$file_size')"; $r = mysqli_query ($dbc, $q); $desired_dir = '../../customers/' . $customerfolder . '/'; $im = imagecreatefromjpeg($file_tmp); imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50); $newpath = "$desired_dir".$file_name; imagepng($im, $newpath); if(empty($errors)==true){ move_uploaded_file($newpath,"$desired_dir".$file_name); imagedestroy($im); //mysql_query($q); }else{ print_r($errors); } } if(empty($error)){ return 'ok'; }else { return 'kunne ikke uploade!'; } }
Comments
Post a Comment