4

Building a custom module and would like to have this option implemented so user will not be able to upload image if the width and height is not as specified. :P

Found this example:

public function validateUploadFile($filePath) {
    $testsize[] = getimagesize($filePath);
    if (!(getimagesize($filePath)) || ($testsize[0][0] < 900) || ($testsize[0][1] < 900)) {
    Mage::throwException($this->__('Image size must be same or greater than 900px * 900px .'));
}
    $_processor = new Varien_Image($filePath);
    return $_processor->getMimeType() !== null;
}

but not sure if it works (not tested yet). If anyone know how to and where to add this code .... should I rewrite image upload function or ....

Sorry guys, I should mention that I'm talking about admin not customer side.

  • Is it for product image? – Purushotam Sangroula Dec 29 '17 at 17:03
  • Nope, additional images like a banners for example. (custom module) – Rob D. A. Dec 29 '17 at 17:04
  • Can I show you by using js? Or you want it to be server side only? – Purushotam Sangroula Dec 29 '17 at 17:20
  • I would love to have it only on server side, using JS is a piece of cake TBH ;P – Rob D. A. Dec 30 '17 at 18:51
  • Can you share ome code? How your uploader works? – sv3n Feb 21 '18 at 20:16
0

it is possible with jQuery :

      $(document).ready(function(){

     var _URL = window.URL || window.webkitURL;

     $('#file').change(function () {
      var file = $(this)[0].files[0];

      img = new Image();
      var imgwidth = 0;
      var imgheight = 0;
      var maxwidth = 640;
      var maxheight = 640;

      img.src = _URL.createObjectURL(file);
      img.onload = function() {
       imgwidth = this.width;
       imgheight = this.height;

       $("#width").text(imgwidth);
       $("#height").text(imgheight);
       if(imgwidth <= maxwidth && imgheight <= maxheight){

        var formData = new FormData();
        formData.append('fileToUpload', $('#file')[0].files[0]);

        $.ajax({
          url: 'upload_image.php',
          type: 'POST',
          data: formData,
          processData: false,
          contentType: false,
          dataType: 'json',
          success: function (response) {
            if(response.status == 1){
              $("#prev_img").attr("src","upload/"+response.returnText);
              $("#prev_img").show();
              $("#response").text("Upload successfully");
            }else{
              $("#response").text(response.returnText);
            } 
          }
       });
      }else{
       $("#response").text("Image size must be "+maxwidth+"X"+maxheight);
      }
     };
     img.onerror = function() {

      $("#response").text("not a valid file: " + file.type);
     }

 });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.