我试图通过做这样的事情来获得图像的宽度和高度。
$width = imagesx("abc.jpg");
$height = imagesy("abc.jpg");
即使我在文件中只有这两行没有与任何其他文件的链接,我仍然遇到此错误。图像在服务器中,我不知道出了什么问题。有人可以帮帮忙吗?谢谢。
警告:imagesx():提供的参数不是...中的有效图像资源 警告:imagesy():提供的参数不是..
中的有效图像资源
答案 0 :(得分:2)
您需要创建一个图像资源,因为imagesy()
期望它作为第一个参数。可以使用文件名中的imagecreatefromjpeg()
创建:
$image = imagecreatefromjpeg("abc.jpg");
if ($image) {
$height = imagesy($image);
imagedestroy($image);
}
或者,如果您只需要将图像宽度设置为高度,则可以使用getimagesize
函数:
list($width, $height) = getimagesize("abc.jpg");
它接受前面的文件名,不需要创建gd图像资源。
答案 1 :(得分:-1)
class SimpleImage { var $image; var $image_type; var $location; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } //// own stuff added 14/06/2010 function getType($filetype) { if( $filetype == 'image/jpeg' ) { $ext = 'jpg'; } elseif( $filetype == 'image/pjpeg' ) { $ext = 'jpg'; } elseif( $filetype == 'image/gif' ) { $ext = 'gif'; } elseif( $filetype == 'image/png' ) { $ext = 'png'; } return $ext; } function randomise(){ // Get a random set of 3 chars which we will append to the filename to prevent duplicate file names. $keychars = "abcdefghijklmnopqrstuvwxyz0123456789"; $length = 3; $randkey = ""; for ($i=0;$i<$length;$i++) $randkey .= substr($keychars, rand(1, strlen($keychars) ), 1); // Set the name of the file (current time + the random value + . + the file extension) $filename = time().$randkey; return $filename; } function uploadimage($location, $filename, $filetype, $i) { /* print "location: ".$location; print "name: ".$filename; print "type: ".$filetype; print "num: ".$i; die('11'); */ $ext = $this->getType($filetype); $newfilename = $this->randomise(); if($ext <> ""){ $file = $newfilename.".".$ext; $uploadfile = $location.$file; // Move the file to the server. If move is successful store the file info to the database if ((move_uploaded_file($_FILES["image"]["tmp_name"][$i], $uploadfile))or die("Couldn't copy the file!".$_FILES["image"]["tmp_name"][$i])){ return $file; } } } }
?>
<?php
// user like this
/*
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resize(250,400);
$image->resizeToWidth(250);
$image->scale(50);
$image->resizeToHeight(500);
$image->save('picture2.jpg');
*/
?>