首页 \ 问答 \ PHP错误处理(PHP Error handling)

PHP错误处理(PHP Error handling)

谢谢你提前一个。

我目前正在调整/改进我为我的公司从头开始编写的MVC框架。 它比较新,所以它肯定是不完整的。 我需要将错误处理整合到框架中(一切都应该有权访问错误处理),并且它应该能够处理不同类型和级别的错误(用户错误和框架错误)。 我的问题是做这件事的最佳方式和最佳机制是什么? 我知道PHP 5的异常处理和PEAR的不同的错误机制,但我从来没有使用过它们中的任何一个。 我需要一些高效且易于使用的东西。

创建我自己的错误处理还是使用已有的东西会更好? 任何建议,提示,问题当然是受欢迎的。 我最终会认为它以甜头来注册PHP的错误处理程序,所以我只需要抛出错误,然后决定如何处理它以及是否继续。

编辑:对不起,我应该提供有关我想记录的错误类型的更多详细信息。 我正在寻找记录2种主要类型的错误:用户和框架。

对于用户错误,我的意思是诸如错误的URL(404),非法访问受限制的页面等等。我知道我可以重新路由到主页或者脱离JavaScript对话框,但我希望能够通过电子邮件这些错误和更多的用户错误,因为它们变得明显。

通过框架错误我的意思是像无法连接到数据库的东西,有人删除了意外的数据库表或以某种方式删除了一个文件等等。

另外,我会照顾开发和现场服务器处理。


Thank you one and all ahead of time.

I am currently in the process of tweaking/improving a MVC framework I wrote from scratch for my company. It is relatively new, so it is certainly incomplete. I need to incorporate error handling into the framework (everything should have access to error handling) and it should be able to handle different types and levels of errors (User errors and Framework errors). My question is what is the best way and best mechanism to do this? I know of PHP 5 exception handling and PEAR's different error mechanism, but I have never used any of them. I need something efficient and easy to use.

Would it be better to create my own error handling or use something already made? Any suggestions, tips, questions are certainly welcomed. I would ultimately think it sweetness to somehow register the error handler with PHP so that I would just need to throw the error and then decide what to do with it and whether to continue.

EDIT: Sorry, I should of provided a more details about what type of errors I wanted to log. I am looking to log 2 main types of errors: User and Framework.

For user errors, I mean things like bad urls (404), illegal access to restricted pages, etc. I know I could just reroute to the home page or just blurt out a JavaScript dialog box, but I want to be able to elegently handle these errors and add more user errors as they become evident.

By Framework errors I means things like cannot connect to the database, someone deleted a database table on accident or deleted a file somehow, etc.

Also, I will take care of development and live server handling.


原文:https://stackoverflow.com/questions/1081061
更新时间:2023-08-10 15:08

最满意答案

我使用Java ImageIO库( https://jai-imageio.dev.java.net )。 他们并不完美,但可以很简单,并完成工作。 就从CMYK转换到RGB而言,这是我所能想到的最好的。

为您的平台下载并安装ImageIO JAR和本机库。 本地库是必不可少的。 没有它们,ImageIO JAR文件将无法检测到CMYK图像。 最初,我的印象是,本地库会提高性能,但不需要任何功能。 我错了。

我唯一注意到的是,转换的RGB图像有时比CMYK图像轻得多。 如果有人知道如何解决这个问题,我会很感激。

以下是将CMYK图像转换为任何支持格式的RGB图像的一些代码。

谢谢,
兰迪Stegbauer

package cmyk;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;

public class Main
{

    /**
     * Creates new RGB images from all the CMYK images passed
     * in on the command line.
     * The new filename generated is, for example "GIF_original_filename.gif".
     *
     */
    public static void main(String[] args)
    {
        for (int ii = 0; ii < args.length; ii++)
        {
            String filename = args[ii];
            boolean cmyk = isCMYK(filename);
            System.out.println(cmyk + ": " + filename);
            if (cmyk)
            {
                try
                {
                    String rgbFile = cmyk2rgb(filename);
                    System.out.println(isCMYK(rgbFile) + ": " + rgbFile);
                }
                catch (IOException e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    /**
     * If 'filename' is a CMYK file, then convert the image into RGB,
     * store it into a JPEG file, and return the new filename.
     *
     * @param filename
     */
    private static String cmyk2rgb(String filename) throws IOException
    {
        // Change this format into any ImageIO supported format.
        String format = "gif";
        File imageFile = new File(filename);
        String rgbFilename = filename;
        BufferedImage image = ImageIO.read(imageFile);
        if (image != null)
        {
            int colorSpaceType = image.getColorModel().getColorSpace().getType();
            if (colorSpaceType == ColorSpace.TYPE_CMYK)
            {
                BufferedImage rgbImage =
                    new BufferedImage(
                        image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
                ColorConvertOp op = new ColorConvertOp(null);
                op.filter(image, rgbImage);

                rgbFilename = changeExtension(imageFile.getName(), format);
                rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();
                ImageIO.write(rgbImage, format, new File(rgbFilename));
            }
        }
        return rgbFilename;
    }

    /**
     * Change the extension of 'filename' to 'newExtension'.
     *
     * @param filename
     * @param newExtension
     * @return filename with new extension
     */
    private static String changeExtension(String filename, String newExtension)
    {
        String result = filename;
        if (filename != null && newExtension != null && newExtension.length() != 0);
        {
            int dot = filename.lastIndexOf('.');
            if (dot != -1)
            {
                result = filename.substring(0, dot) + '.' + newExtension;
            }
        }
        return result;
    }

    private static boolean isCMYK(String filename)
    {
        boolean result = false;
        BufferedImage img = null;
        try
        {
            img = ImageIO.read(new File(filename));
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage() + ": " + filename);
        }
        if (img != null)
        {
            int colorSpaceType = img.getColorModel().getColorSpace().getType();
            result = colorSpaceType == ColorSpace.TYPE_CMYK;
        }

        return result;
    }
}

I use the Java ImageIO libraries (https://jai-imageio.dev.java.net). They aren't perfect, but can be simple and get the job done. As far as converting from CMYK to RGB, here is the best I have been able to come up with.

Download and install the ImageIO JARs and native libraries for your platform. The native libraries are essential. Without them the ImageIO JAR files will not be able to detect the CMYK images. Originally, I was under the impression that the native libraries would improve performance but was not required for any functionality. I was wrong.

The only other thing that I noticed is that the converted RGB images are sometimes much lighter than the CMYK images. If anyone knows how to solve that problem, I would be appreciative.

Below is some code to convert a CMYK image into an RGB image of any supported format.

Thank you,
Randy Stegbauer

package cmyk;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;

public class Main
{

    /**
     * Creates new RGB images from all the CMYK images passed
     * in on the command line.
     * The new filename generated is, for example "GIF_original_filename.gif".
     *
     */
    public static void main(String[] args)
    {
        for (int ii = 0; ii < args.length; ii++)
        {
            String filename = args[ii];
            boolean cmyk = isCMYK(filename);
            System.out.println(cmyk + ": " + filename);
            if (cmyk)
            {
                try
                {
                    String rgbFile = cmyk2rgb(filename);
                    System.out.println(isCMYK(rgbFile) + ": " + rgbFile);
                }
                catch (IOException e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    /**
     * If 'filename' is a CMYK file, then convert the image into RGB,
     * store it into a JPEG file, and return the new filename.
     *
     * @param filename
     */
    private static String cmyk2rgb(String filename) throws IOException
    {
        // Change this format into any ImageIO supported format.
        String format = "gif";
        File imageFile = new File(filename);
        String rgbFilename = filename;
        BufferedImage image = ImageIO.read(imageFile);
        if (image != null)
        {
            int colorSpaceType = image.getColorModel().getColorSpace().getType();
            if (colorSpaceType == ColorSpace.TYPE_CMYK)
            {
                BufferedImage rgbImage =
                    new BufferedImage(
                        image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
                ColorConvertOp op = new ColorConvertOp(null);
                op.filter(image, rgbImage);

                rgbFilename = changeExtension(imageFile.getName(), format);
                rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();
                ImageIO.write(rgbImage, format, new File(rgbFilename));
            }
        }
        return rgbFilename;
    }

    /**
     * Change the extension of 'filename' to 'newExtension'.
     *
     * @param filename
     * @param newExtension
     * @return filename with new extension
     */
    private static String changeExtension(String filename, String newExtension)
    {
        String result = filename;
        if (filename != null && newExtension != null && newExtension.length() != 0);
        {
            int dot = filename.lastIndexOf('.');
            if (dot != -1)
            {
                result = filename.substring(0, dot) + '.' + newExtension;
            }
        }
        return result;
    }

    private static boolean isCMYK(String filename)
    {
        boolean result = false;
        BufferedImage img = null;
        try
        {
            img = ImageIO.read(new File(filename));
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage() + ": " + filename);
        }
        if (img != null)
        {
            int colorSpaceType = img.getColorModel().getColorSpace().getType();
            result = colorSpaceType == ColorSpace.TYPE_CMYK;
        }

        return result;
    }
}

相关问答

更多
  • 那明显是错的。 所有RGB颜色可以表示为CMYK,但不是相反。 您发送的链接有效,但看起来R,G,B三个分量的范围是[0,255],而四个C,M,Y,K分量的范围是[0,1]。 通常情况下,RGB坐标用整数表示,CMYK用浮点数表示。 几乎所有可以在网络上找到的算法都可以像这样工作。 PS:这里是该算法的示例实现, http://www.javascripter.net/faq/rgb2cmyk.htm 。 编辑 : 恐怕详细解释可能会很长,但我们现在就去。 一方面,你有每种颜色的组成部分。 另一方面,您在 ...
  • 我使用Java ImageIO库( https://jai-imageio.dev.java.net )。 他们并不完美,但可以很简单,并完成工作。 就从CMYK转换到RGB而言,这是我所能想到的最好的。 为您的平台下载并安装ImageIO JAR和本机库。 本地库是必不可少的。 没有它们,ImageIO JAR文件将无法检测到CMYK图像。 最初,我的印象是,本地库会提高性能,但不需要任何功能。 我错了。 我唯一注意到的是,转换的RGB图像有时比CMYK图像轻得多。 如果有人知道如何解决这个问题,我会很感 ...
  • 具有多个参数的Haskell函数在某种程度上有点奇怪(但是,在你已经习惯了,非常有用之后)。 rgb2cmyk rgb = ...实际上只是编写lambda表达式的一种简短方法rgb2cmyk = \r -> \g -> \b -> ... ,即这实际上是三个函数的嵌套 ,每个函数只需要一个论点。 因此,这个功能的类型确实如此 rgb2cmyk :: Int -> (Int -> (Int -> ...)) 由于此模式在Haskell中非常常见,因此定义了解析规则,因此您可以省略这些括号。 因此,您尝试定 ...
  • 看看这里 : setImageColorspace (imagick::COLORSPACE_RGB); if ($img->getImageColorspace() == Imagick::COLORSPACE_CMYK) { $profiles = $img->getImageProfiles('*', false); // we're only interest ...
  • (免责声明,我为Atalasoft工作) Atalasoft dotImage将以CMYK的形式读写图像,并在CMYK空间中执行叠加操作。 你需要做的代码是: public void OverlayCMYKOnCMYK(Stream bottomStm, Stream topStm, Point location, Steam outStm) { using (AtalaImage bottom = new AtalaImage(bottomStm, null), top = new AtalaIm ...
  • 只是我的问题的更新。 使用ImageMagick软件,我解决了所有问题。 干杯, Bigster Just an update of my problem. Use ImageMagick software and i get all my problems resolved. Cheers, Bigster
  • 这是一个非常广泛的问题,如果您阅读至少部分PDF规范,对您来说会更好。 为了让你体会我为什么这么说...... PDF和色彩空间 1)PDF可以包含各种颜色空间 - 设备颜色空间,如RGB,CMYK和灰色 - Lab等抽象色彩空间 - 基于ICC配置文件的色彩空间,如基于ICC的RGB,基于ICC的实验室,...... - 命名或特殊颜色空间,如分离,设备-N和N通道 (而且我省略了一些迷人的东西,如图案和阴影) 2)所有上述颜色空间都可以在整个PDF文件中使用。 当您的PDF文件符合某些ISO标准(例如P ...
  • 这个问题的答案类似于C#使用ICC配置文件将RGB值转换为CMYK的答案? : 我不知道任何可以实现此目的的C#API或库。 但是,如果您有足够的C / C ++知识来构建C#的包装器,我会看到两个选项: Windows Color System(WCS) (Windows的一部分) LittleCMS 两个系统都提供API来检查颜色是否可以在其他颜色空间中呈现。 The answer for this question is similar to the answer for C# convert RGB ...
  • 下面的代码将在r存储零,因为r的初始值小于255并且r是int。 因此,按整数除法, r/255将为零。 r = r/255; 相反,你可以将除法的结果存储在一个double变量中,尝试下面的(确保除法中至少有一个操作数是double,否则你可以将它转换为double) double rC = r/255.0; c = ((w-rC)/w); The below code will store zero in r as the initial value of r is less than 255 an ...
  • 这个错误实际上是一个bug;) 我报告了一些,其他一些已经证实了我的恐惧,现在它被分配给开发人员进行修复: http : //pecl.php.net/bugs/bug.php?id = 22184 此时解决方案是使用不同版本的库。 The error in fact it's a bug ;) I reported it, some other has confirmed my fear and now it's assigned to a developer for a fix: http://pecl ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。