giovedì 7 settembre 2017

Usare livecode server per modificare le immagini

Livecode server permette anche di lavorare le immagini come nei programmi normali generati da livecode.
Il seguente codice per esempio vi permette di caricare un'immagine e modificarla, vi servono solo sue file, la pagina iniziale chiamata manip.lc, così scritta:

<?lc
start session


## enter the image file into our $_SESSION if one's just been
# uploaded
if $_FILES["imagefile"] is not empty then
   put url("binfile:" & $_FILES["imagefile"]["filename"]) into $_SESSION["imagedata"]
end if

if $_SESSION["imagedata"] is empty then
   printForm ## no image has been uploaded so display the form to upload one
else
   create image "myImage"
   set the lockLoc of image "myImage" to true
   set the text of image "myImage" to $_SESSION["imagedata"]
   
   repeat for each key tOption in $_POST
      if $_POST[tOption] is not empty then applyOption tOption
   end repeat
   
   if $_SESSION["random"] is empty then
      put random(10000000) into $_SESSION["random"]
   end if
   export snapshot from image "myImage" to file ($_SESSION["random"] & ".png") as PNG
   
   put "<img src=" & quote & ($_SESSION["random"] & ".png") & quote & "></img><br><br>"
   
   put url("binfile:" & $_SESSION["random"] & ".png") into $_SESSION["imagedata"]
   
   printOptions
end if

## apply an option to the image
on applyOption pOption   
   switch pOption
      case "width"
         if $_POST["width"] is not a number then
            put "WIDTH NEEDS TO A NUMBER<br>"
         else
            set the width of image "myImage" to $_POST["width"]
         end if
         break
      case "height"
         if $_POST["height"] is not a number then
            put "HEIGHT NEEDS TO BE A NUMBER<br>"
         else
            set the height of image "myImage" to $_POST["height"]
         end if
         break
      case "huestrip"
         put the imageData of image "myImage" into tImageDat
         repeat with x = $_POST["huestrip"] to the number of chars in tImageDat step 4
            put numtochar(0) into char x of tImageDat
         end repeat
         set the imageData of image "myImage" to tImageDat
         break
      case "hueadd"
         if ($_POST["hueadd"] >= 0) and ($_POST["hueadd"] <= 255) then
            put the imageData of image "myImage" into tImageDat
            repeat with x = $_POST["hueadd"] to the number of chars in tImageDat step 4
               put numtochar($_POST["saturation"]) into char x of tImageDat
            end repeat
            set the imageData of image "myImage" to tImageDat
         else
            put "NEEDS TO BE AN INTEGER 0-255"
         end if
         break
   end switch   
end applyOption

## print photo manipulation options
on printOptions
   
   put "<p>Set image dimensions<br>"
   put "<p><form action='manip.lc' method='post'>"
   put "<input type='text' name='width'>"
   put "<input type='text' name='height'>"
   put "<input type='submit' value='Apply'>"
   put "</form></p>"
   
   
   put "<p>Strip a hue from the image<br>"
   put "<form action='manip.lc' method='post'>"
   put "<input type='radio' name='huestrip' value='2'>Red"
   put "<input type='radio' name='huestrip' value='3'>Green"
   put "<input type='radio' name='huestrip' value='4'>Blue"
   put "<input type='submit' value='apply'>"
   put "</form></p>"
   
   put "<p>Add Color<br>"
   put "<form action='manip.lc' method='post'>"
   put "<input type='radio' name='hueadd' value='2'>Red"
   put "<input type='radio' name='hueadd' value='3'>Green"
   put "<input type='radio' name='hueadd' value='4'>Blue"
   put "<input type='text' name='saturation'>Saturation"
   put "<input type='submit' value='apply'>"
   put "</form></p>"
   
   put "<a href='logout.lc'>Reset Session</a>"   
end printOptions

## print upload form
on printForm
   put "Upload an image:<br>"
   put "<form action='manip.lc' enctype='multipart/form-data' method='post'>"
   put "<input type='file' name='imagefile'><br>"
   put "<input type='submit' value='upload'>"
   put "</form>"
end printForm

stop session
?>

Che vi serve per caricare l'immagine e modificarla come ci pare:



e poi una pagina per distruggere la sessione che contiene l'immagine altri dati, chiamata logout.lc:
<?lc 
 delete session 
 put "<b>Session deleted</b><br>" 
 ?> 
<a href=manip.lc > Start again</a>

Tutto testato e verificato: funziona!

Nessun commento:

Posta un commento