GVKun编程网logo

php – 如何保存裁剪的图像(php裁剪图片)

10

在本文中,我们将给您介绍关于php–如何保存裁剪的图像的详细内容,并且为您解答php裁剪图片的相关问题,此外,我们还将为您提供关于android–从ImageView获取裁剪的图像、android-我

在本文中,我们将给您介绍关于php – 如何保存裁剪的图像的详细内容,并且为您解答php裁剪图片的相关问题,此外,我们还将为您提供关于android – 从ImageView获取裁剪的图像、android-我只想将裁剪后的图像保存到图库中.但会保存原始的完整图像.如何仅保存裁切后的图像?、CSS 显示调整大小和裁剪的图像、CSS显示调整大小并裁剪的图像的知识。

本文目录一览:

php – 如何保存裁剪的图像(php裁剪图片)

php – 如何保存裁剪的图像(php裁剪图片)

从以下链接下载用于裁剪图像的代码:

http://deepliquid.com/content/Jcrop.html

这是crop.PHP页面中的代码:

<?PHP

/**
 * Jcrop image cropping plugin for jQuery
 * Example cropping script
 * @copyright 2008-2009 Kelly Hallman
 * More info: http://deepliquid.com/content/Jcrop_Implementation_Theory.html
 */

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w,$targ_h );

    imagecopyresampled($dst_r,$img_r,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);

    exit;
}

// If not a POST request,display page below:

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.Jcrop.js"></script>
        <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
        <script language="Javascript">

            $(function(){

                $('#cropBox').Jcrop({
                    aspectRatio: 1,onSelect: updateCoords
                });

            });

            function updateCoords(c)
            {
                $('#x').val(c.x);
                $('#y').val(c.y);
                $('#w').val(c.w);
                $('#h').val(c.h);
            };

            function checkCoords()
            {
                if (parseInt($('#w').val())) return true;
                alert('Please select a crop region then press submit.');
                return false;
            };

        </script>

</head>

<body>
<!-- This is the image we're attaching Jcrop to -->
        <img src="demo_files/pool.jpg" id="cropBox" />

        <!-- This is the form that our event handler fills -->
        <form action="crop.PHP" method="post" onsubmit="return checkCoords();">
            <input type="hidden" id="x" name="x" />
            <input type="hidden" id="y" name="y" />
            <input type="hidden" id="w" name="w" />
            <input type="hidden" id="h" name="h" />
            <input type="submit" value="Crop Image" />
        </form>
</body>
</html>

完美裁剪并成功显示裁剪后的图像.

但我想将裁剪后的图像保存到目录中.

我怎样才能做到这一点?

解决方法

试试这个:

imagejpeg($dst_r,'yourfilename.jpg',$jpeg_quality);

// Remove from memory - don't forget this part
imagedestroy($dst_r);

更多信息
http://www.php.net/manual/en/function.imagejpeg.php

android – 从ImageView获取裁剪的图像

android – 从ImageView获取裁剪的图像

我使用 ImageView裁剪位图.
一旦它被裁剪,我想得到裁剪的图像,但我得到与原始图像相同的图像.
有没有办法只获得图像的可视部分?

ImageView iv = new  ImageView(this);                        
iv.setimageBitmap(OriginalBitmap);
iv.setScaleType(ScaleType.CENTER_CROP);
Bitmap CroppedBitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();

CroppedBitmap的值与OriginalBitmap一样.我怎样才能得到裁剪的?

解决方法

这 example对我有用 iv.setDrawingCacheEnabled(真); 位图croppedBitmap = iv.getDrawingCache()

android-我只想将裁剪后的图像保存到图库中.但会保存原始的完整图像.如何仅保存裁切后的图像?

android-我只想将裁剪后的图像保存到图库中.但会保存原始的完整图像.如何仅保存裁切后的图像?

public class ImageCroppMainActivity extends Activity implements OnClickListener{

final int CAMERA_CAPTURE = 1;

//keep track of cropping intent

final int PIC_CROP = 2;

//captured picture uri

private Uri picUri;

final int PICK_IMAGE = 3;   

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_image_cropp_main); 

    //retrieve a reference to the UI button

    Button captureBtn = (Button)findViewById(R.id.capture_btn);

    //handle button clicks

    captureBtn.setonClickListener(this);

    Button browseBtn = (Button)findViewById(R.id.browse_btn);

    //handle button clicks

    browseBtn.setonClickListener(this);

}



public void onClick(View v) {

    if (v.getId() == R.id.capture_btn) {   

        try {

            //use standard intent to capture an image

            Intent captureIntent = new 
Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            //we will handle the returned data in onActivityResult

startActivityForResult(captureIntent, CAMERA_CAPTURE);

}

catch(ActivityNotFoundException anfe){

//display an error message

String errorMessage = "Whoops - your device doesn't support capturing images!";

Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);

toast.show();

}  

}

else if(v.getId() == R.id.browse_btn)

{

//use standard intent to capture an image

Intent intent = new Intent(Intent.ACTION_PICK, 

android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

//we will handle the returned data in onActivityResult

startActivityForResult(intent, PICK_IMAGE);

}

}

/**

 * Handle user returning from both capturing and cropping the image

 */

protected void onActivityResult(int requestCode, int resultCode, Intent 

data) {

    if (resultCode == RESULT_OK) {

        //user is returning from capturing an image using the camera

        if(requestCode == CAMERA_CAPTURE){

            //get the Uri for the captured image

            picUri = data.getData();

            //carry out the crop operation

            performCrop();

        }

        //user is returning from cropping the image

        else if(requestCode == PIC_CROP){

            //get the returned data

            Bundle extras = data.getExtras();

            //get the cropped bitmap

            Bitmap thePic = extras.getParcelable("data");

            //retrieve a reference to the ImageView

            ImageView picView = 

 (ImageView)findViewById(R.id.picture);

            //display the returned cropped image
            MediaStore.Images.Media.insertimage(getContentResolver(), thePic, "" , "");

            picView.setimageBitmap(thePic);

        }

        else if(requestCode == PICK_IMAGE)

        {

 picUri = data.getData();

            //carry out the crop operation

            performCrop();

        }}}

   /**

 * Helper method to carry out crop operation

 */

  private void performCrop(){

    //take care of exceptions

    try {

 //call the standard crop action intent (the user device may not support it)

        Intent cropIntent = new 

   Intent("com.android.camera.action.CROP"); 

   //indicate image type and Uri

        cropIntent.setDataAndType(picUri, "image/*");

        //set crop properties

        cropIntent.putExtra("crop", "false");

        //indicate aspect of desired crop

        cropIntent.putExtra("aspectX", 1);

        cropIntent.putExtra("aspectY", 1);

        //indicate output X and Y

        cropIntent.putExtra("outputX", 256);

        cropIntent.putExtra("outputY", 256);

        //retrieve data on return

        cropIntent.putExtra("return-data", true);

        //start the activity - we handle returning in onActivityResult

        startActivityForResult(cropIntent, PIC_CROP);  

     }

    //respond to users whose devices do not support the crop action

    catch(ActivityNotFoundException anfe){

        //display an error message

        String errorMessage = "Whoops - your device doesn't support the

   crop action!";

        Toast toast = Toast.makeText(this, errorMessage, 

   Toast.LENGTH_SHORT);

        toast.show();

    }

   }

  }

解决方法:

只需3个步骤即可完成-

1)Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="camera.test.demo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SimpleCameragalleryDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

2)SimpleCameragallery演示代码

package camera.test.demo;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SimpleCameragalleryDemo extends Activity {
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_galLERY = 2;
ImageView imgview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

imgview = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);`enter code here`
Button buttongallery = (Button) findViewById(R.id.btn_select_gallery);
buttonCamera.setonClickListener(new View.OnClickListener() {

public void onClick(View v) {
// call android default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);

try {

intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);

} catch (ActivityNotFoundException e) {
// Do nothing for Now
}
}
});
buttongallery.setonClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// Todo Auto-generated method stub
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);

try {

intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_galLERY);

} catch (ActivityNotFoundException e) {
// Do nothing for Now
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
imgview.setimageBitmap(photo);

}
}

if (requestCode == PICK_FROM_galLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable("data");
imgview.setimageBitmap(photo);

}
}
}
}

3)main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewAddCard"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Take Image"
android:textSize="16dp"
android:layout_gravity="center"
android:gravity="center"
android:typeface="sans"/>

<Button
android:id="@+id/btn_take_camera"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Take From Camera"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:typeface="sans"/>

<Button
android:id="@+id/btn_select_gallery"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Select from gallery"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:typeface="sans" />
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

希望对您有帮助.

CSS 显示调整大小和裁剪的图像

CSS 显示调整大小和裁剪的图像

我想显示来自具有特定宽度和高度的 URL 的图像,即使它具有不同的大小比例。所以我想调整大小(保持比例),然后将图像剪切到我想要的大小。

我可以使用 htmlimg属性调整大小,也可以使用background-image.
我怎样才能做到这两点?

例子:

这个图片:

在此处输入图像描述

有大小800x600像素,我想像200x100像素图像一样显示

img可以调整图像大小200x150px

<imgsrc="http://i.stack.imgur.com/wPh0S.jpg">

这给了我这个:

<imgsrc="https://i.stack.imgur.com/wPh0S.jpg">

并且background-image我可以剪切图像200x100像素。

<div>&nbsp;</div>

给我:

    <div>&nbsp;</div>

我怎样才能做到这两点?
调整图像大小,然后将其剪切成我想要的大小?

答案1

小编典典

您可以结合使用这两种方法,例如。

    .crop {        width: 200px;        height: 150px;        overflow: hidden;    }    .crop img {        width: 400px;        height: 300px;        margin: -75px 0 0 -100px;    }    <div>        <img src="https://i.stack.imgur.com/wPh0S.jpg" alt="Donald Duck">    </div>

您可以使用负片margin<div/>.

CSS显示调整大小并裁剪的图像

CSS显示调整大小并裁剪的图像

我想显示具有特定宽度和高度的URL的图像,即使它
具有不同的尺寸比例。所以我想调整大小(保持比例),
然后将图像切成我想要的大小。

我可以使用html img属性调整大小,也可以使用剪切background-image。
我该怎么办?

例:

这个图片:

具有800x600像素大小,我想显示为200x100
像素图像

通过img我可以调整图像大小200x150px:

<imgsrc="http://i.stack.imgur.com/wPh0S.jpg">

这给了我这个:

<imgsrc="https://i.stack.imgur.com/wPh0S.jpg">

而且background-image我可以削减图像200x100像素。

<div>&nbsp;</div>

给我:

<div>&nbsp;</div>

我该怎么办?
调整图像大小,然后将其切成我想要的大小?

关于php – 如何保存裁剪的图像php裁剪图片的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于android – 从ImageView获取裁剪的图像、android-我只想将裁剪后的图像保存到图库中.但会保存原始的完整图像.如何仅保存裁切后的图像?、CSS 显示调整大小和裁剪的图像、CSS显示调整大小并裁剪的图像等相关内容,可以在本站寻找。

本文标签: