Write a Program to upload image with extension gif or jpeg. Write a PHP script to create watermarks using Imagecopymerge. Write a PHP script to convert images to grayscale || DWPD Sem 5 Practical 19
DWPD : Practical 19-A
Write a Program to upload image with extension gif or jpeg
DWPD : Practical 19-B
Write a PHP script to create watermarks using Imagecopymerge
DWPD : Practical 19-C
Write a PHP script to convert images to grayscale
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Output :
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image<br>File Type - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if($check["mime"]=="image/gif" or $check["mime"]=="image/jpeg")
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "<br>Your file <b>". basename( $_FILES["fileToUpload"]["name"]). "</b> has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
else
{
echo "<h3>Only .gif And .jpeg files allowed</h3>";
}
}
?>
Output :
Write a PHP script to create watermarks using Imagecopymerge
<?php
$sourceImage = 'wtr.jpg';
$destImage = 'sunset.jpg';
list($srcWidth, $srcHeight) = getimagesize($sourceImage);
$src = imagecreatefromjpeg($sourceImage);
$dest = imagecreatefromjpeg($destImage);
imagecolortransparent($src,imagecolorat($src,0,0));
imagecopymerge($dest,$src,10,10,0,0,$srcWidth,$srcHeight,50);
imagejpeg($dest,'use-imagecopymerge()-to-watermark-an-image-with-php.jpg',100);
imagedestroy($src);
imagedestroy($dest);
?>
Output :
Write a PHP script to convert images to grayscale
<?php
$source_file="sr.jpg";
$im=ImageCreateFromJpeg($source_file);
$imgw=imagesx($im);
$imgh=imagesy($im);
for($i=0;$i<$imgw;$i++)
{
for($j=0;$j<$imgh;$j++)
{
$rgb=ImageColorAt($im,$i,$j);
$rr=($rgb >> 16) & 0xFF;
$gg=($rgb >> 8) & 0xFF;
$bb=$rgb & 0xFF;
$g=round(($rr+$gg+$bb)/3);
$val=imagecolorallocate($im,$i,$j,$val);
imagesetpixel($im,$i,$j,$val);
}
}
header('Content-type: image/jpeg');
imagejpeg($im);
?>
Comments
Post a Comment