2017-01-07 29 views
-1

有没有可能在PHP中缩放图像? 我在谷歌搜索,但没有发现实用程序。如何使用php缩放图像

如果有人有任何提示,请给我。谢谢

+0

为了什么?是否用于查看?因为如果是这样你可以在客户端使用JavaScript。如果你想用放大的图片替换图片,你可以在PHP中使用[**'imagecopyresampled' **](http://php.net/manual/en/function.imagecopyresampled.php) – Phiter

+0

为了观看我想要。谢谢 – johnwilliam25

+0

您是否使用javscript传递图片(至php?) –

回答

1

Imagecopyresampled这里的解决方案:

$zoom=120; //to zoom 120% 
$width = imagesx($myimage); 
$height = imagesy($myimage); 

$new_width = $width * $zoom/100; 
$new_height = $height * $zoom/100; 

imagecopyresampled($image_p, $myimage, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
+0

谁是$ image_p? – johnwilliam25

2

如果按照imagecopyresampled PHP示例(并使其大,而不是更小 - (小演示了php.net的例子),如果你是路过一个js变量,那么你会得到类似下面的PHP:

<?php 
// The file 
$imgin = $_POST["dogimg"]; 
     //e.g if your img is called dogimg! 
$percent = 1.4; //140% 

// Content type 
header('Content-Type: image/jpeg'); 

// Get new dimensions 
list($width, $height) = getimagesize($imgin); 
$new_width = $width * $percent; 
$new_height = $height * $percent; 

// Resample 
$image_out = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($imgin); 
imagecopyresampled($image_out, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// Output 
imagejpeg($image_out, null, 100); 
?> 

不过,我要指出的是与文件类型,如图像时,你要小心,我会建议你把一些error handlingcheck that the file is an image(如果您通过网站上传图片,最好在上传图片时首先进行)。

如果您正在使用您上传到服务器上的预先上传的图片,那么您应该很好。

+0

有关mime类型的更多信息:http://php.net/manual/en/function.mime-content-type.php –

2

使用imagemagic

$geometry = '142x106'; 
$resize_geo = $geometry.'^'; 
$crop_geo = $geometry.'+0+0'; 
`convert $pic_original_path -resize $resize_geo -gravity Center -crop $crop_geo $pic_resized_path`;