我正在使用OpenCart 1.5.6并编写了各种代码来检查图像的DPI并调整图像大小以便让用户知道他们是否拥有正确的文件。
调整图片大小:http://zing-cards.com/imagetest2.php(orig为640x480)
检查DPI:http://zing-cards.com/imagedpi1.php
正如您所见,它们可以在我的服务器上运行。
调整代码大小:
<?php
$photo="/home/tcsdesig/public_html/zing/flower.jpg";
$cmd = "/usr/bin/convert $photo -thumbnail 300x182 JPG:-"; //Convert image to smaller size for preview
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
检查DPI
<?php
header("Content-type: text/html");
$photoPath1 = "flower1.jpg";
...
$dpiCommand = "/usr/bin/identify";
if (!is_executable($dpiCommand)) die("Sorry, can't execute $dpiCommand!");
$output = shell_exec("$dpiCommand -format '%x' $photoPath1");
$dpi = explode(' ',trim($output)); //breaks up the result to get the number out (72 PixelsPerInch = 72)
$dpiInt = (int)$dpi[0]; //changes the number to an integer (72 PixelsPerInch = 72)
if ($dpiInt < 300) { //check if PPI is less than 300.
echo "You should increase your PPI (DPI) for best printing quality. <br/><br/> Your current PPI is " . $output . "<br/>";
} else {
echo "Congratulations! You're image is optimized for best printing quality.";
}
?>
对于调整大小,我希望OC上传原件,然后调整大小,上传调整大小的副本,然后显示该副本。我开始只是试图显示一个调整大小的副本。在我的控制器中=&gt; product.php我将代码更改为:
if (!$json && is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
$file = basename($filename) . '.' . md5(mt_rand());
// Hide the uploaded file name so people can not link to it directly.
$json['file'] = $this->encryption->encrypt($file);
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file);
/* RESIZE IMAGE */
$resizeCommand = "/usr/bin/convert";
if (!is_executable($resizeCommand)) die("Sorry, can't execute $resizeCommand!");
$resizeOutput = shell_exec("$resizeCommand basename($filename) -thumbnail 300x182 JPG:-");
/* END RESIZE IMAGE */
$json['thumb'] = $resizeOutput; //this gets displayed on the product.tpl page in a div set aside for the image.
$json['success'] = 'The file "' . basename($filename) . '" has been uploaded.';
}
结果返回null并且我没有收到错误消息以查看resizeCommand是否可执行。
有关此代码的详细信息,您可以查看我的相关帖子:Opencart - Customer uploads different files to one product with different responses from the site
我是否在正确的轨道上?这是我应该添加此代码还是应该在product.php中?
这可能是我在这里提到的php.ini或索引文件:https://stackoverflow.com/questions/21324895/image-magick-php-extension-is-installed-but-showing-error-class-imagick-not-foun?
任何建议都表示赞赏。
答案 0 :(得分:0)
如果您尝试以opencart方式调整图片大小,则可以在控制器文件中使用以下代码:
$this->load->model('tool/image');
if ($this->data['profile_pic']) {
$this->data['image'] = $this->model_tool_image->resize($this->data['profile_pic'], 320, 240); //320 width, 240 height
} else {
$this->data['image'] = '';
}
其中$this->data['profile_pic']
- 包含相对于image
文件夹的路径。
度过美好的一天!