Wednesday, January 12, 2011

Image resize without distortion and keeping proportions

Image resize without distortion and keeping proportions.



Image distortion is the normal problem with the image resize in
any programming language. You need to have good knowledge of math to
resizing the image.
I have implemented image resize code without distortion in PHP.
You can easily convert it in the any other programming language
for ex: ASP.Net, JSP etc:

Here is the listed below code:
====================================

img_resize.php

$filename = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQOT3CzejzFpMcWAqDQekKgdvDT8f4Ot1gFRIG3UDWN8N8y7kIMAagI85rho803ugEzu5IgQDK9k8FgDxsjKe8xgJ8zpaW8JaejckiKU3tnhYy48q0m2eavUhP9yn9x8cU-t7e4YPepPg/s1600/auto-resume-broken-uploader.jpg";
$nw = 170; // new width
$nh = 128; // new height
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
if($width<= $nw) {
   $nw=$width;
}
$newHeight = $height*$nw/$width; // calculate new height here
if($newHeight > $nh) {
   $nw = $width*$nh/$height;
   $newHeight=$nh;
}
$image_s = imagecreatetruecolor($nw,$newHeight);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_s, $image, 0, 0, 0, 0, $nw,$newHeight,$width,$height);
imagejpeg($image_p, null, 100);   


use it as listed below:
<img src="img_resize.php" />

Note that you can also make the parameter for height and width.







Saturday, January 1, 2011

Better AlertDialog box in Android

Better AlertDialog box in Android



package com.mytest.android;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyTestActivity extends Activity {
    ProgressDialog progressDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressDialog = new ProgressDialog(this);
        Button buttonClickMe = (Button)findViewById(R.id.button1);
        buttonClickMe.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View arg0) {
                //progressDialog.setMessage("Please wait ... ");
                //progressDialog.show();
                // do stuff here......
                // to cancel progressDialog box
                //progressDialog.cancel();
                // finally show dialog box here
                MyAlert.displayAlert(MyTestActivity.this , "Notification", "You have pressed on me!!!", null, "Ok");
            }
        });
    }
  
}




=================================================


package com.mytest.android;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
public class MyAlert {
    public static void displayAlert(Context cntx ,String title,String message, final String NegButton,final String posButton) {
        AlertDialog.Builder alertBuilder = new Builder( cntx);
        alertBuilder.setTitle(title);
        alertBuilder.setMessage(message);
        if(NegButton != null) {
            alertBuilder.setNegativeButton(NegButton,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { }
            });
        }
       
        if (posButton != null) {
            alertBuilder.setPositiveButton(posButton,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
        }
        AlertDialog alert = alertBuilder.create();
        alert.show();
    }
}




Android App