Ver Mensaje Individual
  #18 (permalink)  
Antiguo 01/07/2011, 05:47
konvulsion
 
Fecha de Ingreso: abril-2011
Mensajes: 39
Antigüedad: 13 años, 7 meses
Puntos: 1
Respuesta: Código para meter texto en imagen

A ver, intento hacer esto:

Archivo my_file.php:

Código PHP:
Ver original
  1. <form enctype="multipart/form-data" method="post" action="image_upload_script.php">
  2. Choose your file here:
  3. <input name="uploaded_file" type="file"/><br /><br />
  4. <input type="submit" value="Upload It"/>
  5. </form>

Archivo image_upload_script.php:

Código PHP:
Ver original
  1. <?php
  2. // Access the $_FILES global variable for this specific file being uploaded
  3. // and create local PHP variables from the $_FILES array of information
  4. $fileName = $_FILES["uploaded_file"]["name"]; // The file name
  5. $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
  6. $fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
  7. $fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
  8. $fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
  9. $fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName); // filter
  10. $kaboom = explode(".", $fileName); // Split file name into an array using the dot
  11. $fileExt = end($kaboom); // Now target the last array element to get the file extension
  12.  
  13. // START PHP Image Upload Error Handling -------------------------------
  14. if (!$fileTmpLoc) { // if file not chosen
  15.     echo "ERROR: Please browse for a file before clicking the upload button.";
  16.     exit();
  17. } else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
  18.     echo "ERROR: Your file was larger than 5 Megabytes in size.";
  19.     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  20.     exit();
  21. } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
  22.      // This condition is only if you wish to allow uploading of specific file types    
  23.      echo "ERROR: Your image was not .gif, .jpg, or .png.";
  24.      unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  25.      exit();
  26. } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
  27.     echo "ERROR: An error occured while processing the file. Try again.";
  28.     exit();
  29. }
  30. // END PHP Image Upload Error Handling ---------------------------------
  31. // Place it into your "uploads" folder mow using the move_uploaded_file() function
  32. $moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
  33. // Check to make sure the move result is true before continuing
  34. if ($moveResult != true) {
  35.     echo "ERROR: File not uploaded. Try again.";
  36.     exit();
  37. }
  38. // Include the file that houses all of our custom image functions
  39. include_once("ak_php_img_lib_1.0.php");
  40. // ---------- Start Adams Universal Image Resizing Function --------
  41. $target_file = "uploads/$fileName";
  42. $resized_file = "uploads/resized_$fileName";
  43. $wmax = 500;
  44. $hmax = 500;
  45. ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
  46. // ----------- End Adams Universal Image Resizing Function ----------
  47. // ---------- Start Adams Convert to JPG Function --------
  48. if (strtolower($fileExt) != "jpg") {
  49.     $target_file = "uploads/resized_$fileName";
  50.     $new_jpg = "uploads/resized_".$kaboom[0].".jpg";
  51.     ak_img_convert_to_jpg($target_file, $new_jpg, $fileExt);
  52. }
  53. // ----------- End Adams Convert to JPG Function -----------
  54. // ---------- Start Adams Image Watermark Function --------
  55. $target_file = "uploads/resized_".$kaboom[0].".jpg";
  56. $wtrmrk_file = "watermark.png";
  57. $new_file = "uploads/protected_".$kaboom[0].".jpg";
  58. ak_img_watermark($target_file, $wtrmrk_file, $new_file);
  59. // ----------- End Adams Image Watermark Function -----------
  60. // Display things to the page so you can see what is happening for testing purposes
  61. echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
  62. echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
  63. echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
  64. echo "The file extension is <strong>$fileExt</strong><br /><br />";
  65. echo "The Error Message output for this upload is: $fileErrorMsg";
  66. ?>

Archivo ak_php_img_lib_1.0.php:

Código PHP:
Ver original
  1. <?php
  2. // Adam Khoury PHP Image Function Library 1.0
  3. // ----------------------- RESIZE FUNCTION -----------------------
  4. // Function for resizing any jpg, gif, or png image files
  5. function ak_img_resize($target, $newcopy, $w, $h, $ext) {
  6.     list($w_orig, $h_orig) = getimagesize($target);
  7.     $scale_ratio = $w_orig / $h_orig;
  8.     if (($w / $h) > $scale_ratio) {
  9.            $w = $h * $scale_ratio;
  10.     } else {
  11.            $h = $w / $scale_ratio;
  12.     }
  13.     $img = "";
  14.     $ext = strtolower($ext);
  15.     if ($ext == "gif"){
  16.     $img = imagecreatefromgif($target);
  17.     } else if($ext =="png"){
  18.     $img = imagecreatefrompng($target);
  19.     } else {
  20.     $img = imagecreatefromjpeg($target);
  21.     }
  22.     $tci = imagecreatetruecolor($w, $h);
  23.     // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
  24.     imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
  25.     if ($ext == "gif"){
  26.         imagegif($tci, $newcopy);
  27.     } else if($ext =="png"){
  28.         imagepng($tci, $newcopy);
  29.     } else {
  30.         imagejpeg($tci, $newcopy, 84);
  31.     }
  32. }
  33. // -------------- THUMBNAIL (CROP) FUNCTION ---------------
  34. // Function for creating a true thumbnail cropping from any jpg, gif, or png image files
  35. function ak_img_thumb($target, $newcopy, $w, $h, $ext) {
  36.     list($w_orig, $h_orig) = getimagesize($target);
  37.     $src_x = ($w_orig / 2) - ($w / 2);
  38.     $src_y = ($h_orig / 2) - ($h / 2);
  39.     $ext = strtolower($ext);
  40.     $img = "";
  41.     if ($ext == "gif"){
  42.     $img = imagecreatefromgif($target);
  43.     } else if($ext =="png"){
  44.     $img = imagecreatefrompng($target);
  45.     } else {
  46.     $img = imagecreatefromjpeg($target);
  47.     }
  48.     $tci = imagecreatetruecolor($w, $h);
  49.     imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
  50.     if ($ext == "gif"){
  51.         imagegif($tci, $newcopy);
  52.     } else if($ext =="png"){
  53.         imagepng($tci, $newcopy);
  54.     } else {
  55.         imagejpeg($tci, $newcopy, 84);
  56.     }
  57. }
  58. // ----------------------- IMAGE CONVERT FUNCTION -----------------------
  59. // Function for converting GIFs and PNGs to JPG upon upload
  60. function ak_img_convert_to_jpg($target, $newcopy, $ext) {
  61.     list($w_orig, $h_orig) = getimagesize($target);
  62.     $ext = strtolower($ext);
  63.     $img = "";
  64.     if ($ext == "gif"){
  65.         $img = imagecreatefromgif($target);
  66.     } else if($ext =="png"){
  67.         $img = imagecreatefrompng($target);
  68.     }
  69.     $tci = imagecreatetruecolor($w_orig, $h_orig);
  70.     imagecopyresampled($tci, $img, 0, 0, 0, 0, $w_orig, $h_orig, $w_orig, $h_orig);
  71.     imagejpeg($tci, $newcopy, 84);
  72. }
  73. // ----------------------- IMAGE WATERMARK FUNCTION -----------------------
  74. // Function for applying a PNG watermark file to a file after you convert the upload to JPG
  75. function ak_img_watermark($target, $wtrmrk_file, $newcopy) {
  76.     $watermark = imagecreatefrompng($wtrmrk_file);
  77.     imagealphablending($watermark, false);
  78.     imagesavealpha($watermark, true);
  79.     $img = imagecreatefromjpeg($target);
  80.     $img_w = imagesx($img);
  81.     $img_h = imagesy($img);
  82.     $wtrmrk_w = imagesx($watermark);
  83.     $wtrmrk_h = imagesy($watermark);
  84.     $dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
  85.     $dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
  86.     imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
  87.     imagejpeg($img, $newcopy, 100);
  88.     imagedestroy($img);
  89.     imagedestroy($watermark);
  90. }
  91. ?>

Se supone que es exactamente lo que quiero, insertar un dibujo que se me visualiza junto con una marca de agua (no hay strings, pero supongo que no costaría nada ponerlos). El problema es que no consigo que se me suba bien el dibujo sin un maldito mensaje de error, y eso que hay un video explicativo y todo aparte que he visto, pero yo veo que está todo igual. No quiero hacerme pesado, si tengo que ofrecer dinero lo ofrezco, 20 o 30 euros, pero es que llega un momento que ya no sé qué sustituir por qué, si sustituyo el uploads por mi directorio de destino da los mismos errores. Necesitaré alguien que me explique las cosas como los niños pequeños (y pensar que he sido capaz de entender cómo se hace un blog multinivel con bases de datos relacionadas y todo, pero es que con la movida esta del GD no puedo).

http://www.youtube.com/watch?v=PFSkPzJNAho (Este es el video, por si veis algo raro que no está en los script)

Última edición por konvulsion; 01/07/2011 a las 05:53