Showing posts with label PHP Image delete Common Function Jquery Ajax Call. Show all posts
Showing posts with label PHP Image delete Common Function Jquery Ajax Call. Show all posts

Crop Image in Core PHP

Crop Image in Core PHP

Today Discussed Crop Image in Core PHP. Image crop and create thumb image and upload save particular directory. crop this image and resize compressed image save a directory. code follow us. 
Crop Image in Core PHP
Crop Image in Core PHP
$upload_dir = "upload_pic";     // The directory for the images to be saved in
$upload_path = $upload_dir."/";    // The path to where the image will be saved
$large_image_prefix = "resize_";    // The prefix name to large image
$thumb_image_prefix = "thumbnail_";   // The prefix name to the thumb image
$large_image_name = $large_image_prefix.$_SESSION['random_key'];     // New name of the large image (append the timestamp to the filename)
$thumb_image_name = $thumb_image_prefix.$_SESSION['random_key'];     // New name of the thumbnail image (append the timestamp to the filename)
$max_file = "3";        // Maximum file size in MB
$max_width = "500";       // Max width allowed for the large image
$thumb_width = "100";      // Width of thumbnail image
$thumb_height = "100";      // Height of thumbnail image
// Only one of these image types should be allowed for upload
$allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
$allowed_image_ext = array_unique($allowed_image_types); // do not change this
$image_ext = ""; // initialise variable, do not change this.
foreach ($allowed_image_ext as $mime_type => $ext) {
    $image_ext.= strtoupper($ext)." ";
}

Image Resize Function


function resizeImage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imageType) = getimagesize($image);
 $imageType = image_type_to_mime_type($imageType);
 $newImageWidth = ceil($width * $scale);
 $newImageHeight = ceil($height * $scale);
 $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
 switch($imageType) {
  case "image/gif":
   $source=imagecreatefromgif($image); 
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image); 
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image); 
   break;
   }
 imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
 
 switch($imageType) {
  case "image/gif":
     imagegif($newImage,$image); 
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newImage,$image,90); 
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newImage,$image);  
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
Resize Thumbnail Image Function

function resizeImage($image,$width,$height,$scale) {
 list($imagewidth, $imageheight, $imageType) = getimagesize($image);
 $imageType = image_type_to_mime_type($imageType);
 $newImageWidth = ceil($width * $scale);
 $newImageHeight = ceil($height * $scale);
 $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
 switch($imageType) {
  case "image/gif":
   $source=imagecreatefromgif($image); 
   break;
     case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
   $source=imagecreatefromjpeg($image); 
   break;
     case "image/png":
  case "image/x-png":
   $source=imagecreatefrompng($image); 
   break;
   }
 imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
 
 switch($imageType) {
  case "image/gif":
     imagegif($newImage,$image); 
   break;
       case "image/pjpeg":
  case "image/jpeg":
  case "image/jpg":
     imagejpeg($newImage,$image,90); 
   break;
  case "image/png":
  case "image/x-png":
   imagepng($newImage,$image);  
   break;
    }
 
 chmod($image, 0777);
 return $image;
}
function getHeight($image) {
 $size = getimagesize($image);
 $height = $size[1];
 return $height;
}
//You do not need to alter these functions
function getWidth($image) {
 $size = getimagesize($image);
 $width = $size[0];
 return $width;
}

if (isset($_POST["upload_thumbnail"]) && strlen($large_photo_exists)>0) {
 //Get the new coordinates to crop the image.
 $x1 = $_POST["x1"];
 $y1 = $_POST["y1"];
 $x2 = $_POST["x2"];
 $y2 = $_POST["y2"];
 $w = $_POST["w"];
 $h = $_POST["h"];
 //Scale the image to the thumb_width set above
 $scale = $thumb_width/$w;
 $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
 //Reload the page again to view the thumbnail
 header("location:".$_SERVER["PHP_SELF"]);
 exit();
}


if ($_GET['a']=="delete" && strlen($_GET['t'])>0){
//get the file locations 
 $large_image_location = $upload_path.$large_image_prefix.$_GET['t'];
 $thumb_image_location = $upload_path.$thumb_image_prefix.$_GET['t'];
 if (file_exists($large_image_location)) {
  unlink($large_image_location);
 }
 if (file_exists($thumb_image_location)) {
  unlink($thumb_image_location);
 }
 header("location:".$_SERVER["PHP_SELF"]);
 exit(); 
}
view.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <meta name="generator" content="WebMotionUK" />
 <title>WebMotionUK - PHP &amp; Jquery image upload &amp; crop</title>
 <script type="text/javascript" src="js/jquery-pack.js"></script>
 <script type="text/javascript" src="js/jquery.imgareaselect.min.js"></script>
</head>
<body>

PHP Image delete Common Function Jquery Ajax Call

PHP Image delete Common Function Jquery Ajax Call


Today Discussed PHP Image delete Common Function Jquery Ajax Call Normal Core PHP Commonly Write one function just put table name and primary id and path get delete the image and unlink this page follows code.

PHP Image delete Common Function Jquery Ajax Call
PHP Image delete Common Function Jquery Ajax Call


Form.php

 <div class="col-md-6" id="delimage">
                                    <label> </label>
                                    <?php if (getprofile('image', $_SESSION['UID']) != '') { ?>
                                                                                        <img src="<?php echo $sitename . 'pages/profile/image/' . getprofile('image', $_SESSION['UID']); ?>" style="padding-bottom:10px;" height="100" /><!--&nbsp;<buttton type="button" style="cursor:pointer;" class="btn btn-danger" name="del" id="del" onclick="javascript:dellocationimage();"><i class="fa fa-close">&nbsp;Delete Image</i></buttton>-->
                                        <button type="button" style="cursor:pointer;" class="btn btn-danger" name="del" id="del" onclick="javascript:deleteimage('<?php echo getprofile('image', $_SESSION['UID']); ?>', '<?php echo $_SESSION['UID']; ?>', 'manageprofile', '../pages/profile/image/', 'image', 'pid');"><i class="fa fa-close">&nbsp;Delete Image</i></button>
                                    <?php } ?>
                                </div>
Ajax.js(Javascript)
function deleteimage(a, b, c, d, e, f)
{
    if (window.XMLHttpRequest)
    {
        oRequestsubcat = new XMLHttpRequest();
    } else if (window.ActiveXObject)
    {
        oRequestsubcat = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (a != '' && b != '' && c != '' && d !='' && e !=''&& f !='')
    {
        document.getElementById("delimage").innerHTML = '<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>';
        oRequestsubcat.open("POST", Settings.base_url + "config/functions_ajax.php", true);
        oRequestsubcat.onreadystatechange = delimg;
        oRequestsubcat.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        oRequestsubcat.send("image=" + a + "&id=" + b + "&table=" +c + "&path=" + d + "&images=" +e + "&pid=" + f);
        console.log(a,b,c,d,e,f);
    }
}
function delimg()
{
    if (oRequestsubcat.readyState == 4)
    {
        if (oRequestsubcat.status == 200)
        {
            document.getElementById("delimage").innerHTML = oRequestsubcat.responseText;
        }
        else
        {
            document.getElementById("delimage").innerHTML = oRequestsubcat.responseText;
        }
    }
}
or

Ajax.js(Jquery)
function deleteimage(a,b,c,d)
{
    $.post(Settings.base_url + "config/functions_ajax.php",{ a:a,b:b,c:c,d:d },function(data){
        console.log(data);
        $('#delimage').html(data);
        //alert(data);
    });
}

Function.php

if (($_REQUEST['image'] != '') && ($_REQUEST['id'] != '') && ($_REQUEST['table'] != '')&& ($_REQUEST['path'] != '')&& ($_REQUEST['images'] != '')&& ($_REQUEST['pid'] != '')) {
    unlink($_REQUEST['path'].$_REQUEST['image']);
 // $s = "UPDATE `".$_REQUEST['table']."` SET `".$_REQUEST['images']."`='' WHERE `".$_REQUEST['pid']."`='" . $_REQUEST['id'] . "'";
   // echo $s;
    //return $s;
   // exit;
    DB("UPDATE `".$_REQUEST['table']."` SET `".$_REQUEST['images']."`='' WHERE `pid`='" . $_REQUEST['id'] . "'");
    DB("INSERT `history` (`page`,`pageid`,`action`,`userid`,`ip`,`actionid`,`info`) VALUES ('Manage Profile','9','Delete','" . $_SESSION['UID'] . "','" . $_SERVER['REMOTE_ADDR'] . "','" . $_REQUEST['b'] . "','Image Deletion')");
  echo '<div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><h4><i class="icon fa fa-close"></i>Succesfully Deleted</h4><!--<a href="' . $sitename . 'settings/adddepartment.htm">Add another one</a>--></div>';